<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层
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.service
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.Controller
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类型的如下
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的配置文件如下
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.启动类
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可以不写这个类)
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();
}
}
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>标签中添加如下配置
<!--指定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>