springboot集成mybatis+jsp

項目結構:

 pom配置文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
  </parent>
  <dependencies>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--jsp頁面使用jstl標籤-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
    </dependency>

    <!--用於編譯jsp-->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
    </dependency>
    <!--引入mysql依賴-->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
    <!-- 去除模板引擎  引用jsp-->
    <!--<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>1.4.0.RELEASE</version>
    </dependency>-->
      <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.5.1.RELEASE</version>
      </dependency>

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <!--<scope>provided</scope>-->
    </dependency>

    <dependency>
      <groupId>net.sourceforge.nekohtml</groupId>
      <artifactId>nekohtml</artifactId>
      <version>1.9.22</version>
    </dependency>

    <!--引入mybatis依賴-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.1</version>
    </dependency>

  </dependencies>

2.dao層html

package com.dao;

import com.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 由於springboot 集成jpa寫sql須要繼承extends JpaRepository<User,Long>
 *     mybatis不須要  因此註釋掉
 */
@Repository
public interface UserDao /*extends JpaRepository<User,Long> */{


    @Select(" select * from User  ")
   // @Query("from user")   jpa語句
    List<User> getAllUser();

    @Insert("insert into user(name,password) VALUES(#{name},#{password})")
    int addUser(User user);
}

3.servicejava

package com.userService;

import com.dao.UserDao;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@org.springframework.stereotype.Service
public class UserServiceImpl implements UserService {

    @Autowired
   private UserDao userdao;

    @Override
    public List<User> getUserList() {
        List<User> list=userdao.getAllUser();
        return list;
    }

    @Override
    public int addUser(User user) {
        int result=userdao.addUser(user);
        return result;
    }
}

4.Controllermysql

package com.coller;

import com.pojo.User;
import com.userService.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/")
    public String index(Model model){

        List<User> list=userService.getUserList();
        model.addAttribute("user",list);
        return "/test";
    }


    @ResponseBody
    @RequestMapping("add")
    public String add(User user){
       int i= userService.addUser(user);
       System.out.print(i);
        return "1" ;
    }
}

5.配置文件爲application.properties類型的以下web

server.port=8888
server.tomcat.uri-encoding=utf-8

#MySQL
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#Spring Data JPA
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

#springmvc
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

 

application.yml的配置文件以下spring

server:
  #端口號
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp


mybatis:
 # mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.bdqn.pojo

6.啓動類sql

package com.Test;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication
@ComponentScan(basePackages = "com")
@MapperScan("com.dao")
public class hehe  extends SpringBootServletInitializer {



    public static void main(String[] args) throws Exception {
        SpringApplication.run(hehe.class, args);
    }
}

7.jpa註解掃描類(若是用mybatis能夠不寫這個類)apache

package com;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories(basePackages = "com.dao")
@EntityScan(basePackages = "com.pojo")
public class JpaConfiguration {
    @Bean
    PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

若是想將mapper文件和dao層放在一塊兒以下圖:

將配置文件修改成

server:
  #端口號
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: XXX
    password: XXX
    url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp


mybatis:
 #指定實體類位置
  type-aliases-package: com.bdqn.pojo

pom文件中<build>標籤中添加以下配置tomcat

<!--指定mapper存放路徑-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <!--指定配置文件存放路徑-->
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
            <include>**/*.yml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
相關文章
相關標籤/搜索