Springboot集成Mybatis、JPA
一、Springboot与Mybatis集成
1、思路 一是引入mybatis-starter使springboot与mybatis集成
二是完成连接数据库
(1)pom.xml 主要是引入mybatis-spring-boot-starter
<!-- mybatis新增 begin--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--mybatis end -->
(2)application.properties 添加相关配置
#mybatis add mybatis.type-aliases-package=com.neusoft.demo.domain spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username = root spring.datasource.password = root
(3)在启动类中添加对mapper包扫描@MapperScan
@SpringBootApplication @MapperScan("com.neusoft.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
(4)新建UserController
package com.neusoft.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.neusoft.demo.domain.User; import com.neusoft.demo.service.UserService; @RestController @RequestMapping("/user") public class UserController { @Autowired UserService userService; @RequestMapping("/findAll") public List<User> findAll(){ return userService.findAll(); } }
(5)新建Service接口和实现类
package com.neusoft.demo.service; import java.util.List; import com.neusoft.demo.domain.User; public interface UserService { List<User> findAll(); }
package com.neusoft.demo.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.neusoft.demo.domain.User; import com.neusoft.demo.mapper.UserMapper; @Service public class UserServiceImpl implements UserService{ @Autowired UserMapper userMapper; @Override public List<User> findAll() { // TODO Auto-generated method stub return userMapper.findAll(); } }
(6)新建UserMapper接口
package com.neusoft.demo.mapper; import java.util.List; import org.apache.ibatis.annotations.Select; import com.neusoft.demo.domain.User; public interface UserMapper { @Select("select id,name from user") List<User> findAll(); }
(7)新建表user,varchar类型id和name
二、springboot与jpa集成
1、添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2、配置数据源和JPA
配置数据源,在application.properties文件中加入如下信息
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username = root spring.datasource.password = root spring.datasource.tomcat.max-active=20
配置JPA,在application.properties配置文件中加入如下信息
#连接哪种数据库 spring.jpa.database=mysql #查询过程中日志里是否显示查询语句 spring.jpa.show-sql=true #自动根据实体创建表 spring.jpa.hibernate.ddl-auto=update
配置说明
ddl-auto:create----每次运行该程序,没有表格会新建表格,表内有数据会清空
ddl-auto:create-drop----每次程序结束的时候会清空表
ddl-auto:update----每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
ddl-auto:validate----运行程序会校验数据与数据库的字段类型是否相同,不同会报错
3、创建实体类
package com.neusoft.demo.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="person") @GenericGenerator(name="jpa-uuid",strategy="uuid") public class Person { @Id @GeneratedValue(generator="jpa-uuid") @Column(length=32) private String id; @Column(name="name",nullable=true,length=20) private String name; @Column(name="age",nullable=true,length=4) private int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
4、创建接口
package com.neusoft.demo.dao; import org.springframework.data.jpa.repository.JpaRepository; import com.neusoft.demo.domain.Person; public interface PersonRepository extends JpaRepository<Person, String>{ }
5、创建controller
package com.neusoft.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.neusoft.demo.dao.PersonRepository; import com.neusoft.demo.domain.Person; @RestController @RequestMapping("/person") public class JPAController { @Autowired PersonRepository personRepository; @RequestMapping("/add") public String addPerson(Person person) { personRepository.save(person); return "success"; } @RequestMapping("/update") public String updatePerson(Person person) { personRepository.save(person); return "success"; } @RequestMapping("/del") public String delPerson(String id) { personRepository.deleteById(id); return "success"; } @RequestMapping("/findAll") public List<Person> findPerson() { return personRepository.findAll(); } }
欢迎关注微信公众号:大数据从业者