整合之前
1、pom.xml添加mysql依赖
mysql
mysql-connector-java
2、application.properties加入数据库连接配置
#################################数据库连接配置########
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stuspringboot
spring.datasource.username=root
spring.datasource.password=root
一、整合JdbcTemplate
1、pom.xml添加相关依赖
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
2、UserServiceImpl使用JdbcTemplate
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional
public User add(String name,Integer age) {
User user = new User(name,age);
jdbcTemplate.update("INSERT INTO user VALUES(?,?,?)", user.getId(), user.getName(), user.getAge());
return user;
}
}
3、 UserController
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/add",method = RequestMethod.POST)
public User add(String name,Integer age){
User user = userService.add(name, age);
return user;
}
}
4、启动项目测试成功
二、整合Mybatis(注解版)
1、pom.xml添加依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
2、UserMapper
public interface UserMapper {
@Select("SELECT * FROM USER WHERE name = #{name}")
User findByName(@Param("name") String name);
}
3、UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findByName(String name) {
return userMapper.findByName(name);
}
}
注入userMapper时,在IDEA环境下会报错,但是不影响使用,如果看着不爽,可以参考https://www.cnblogs.com/waterystone/p/6214212.html 解决
4、UserController添加
@RequestMapping(value = "/{name}",method = RequestMethod.GET)
public User find(@PathVariable String name){
User user = userService.findByName(name);
return user;
}
5、启动类添加@MapperScan
@SpringBootApplication
@MapperScan(basePackages = "com.caofanqi.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6、启动测试如下
7、整合Mybatis分页插件PageHelper
①pom.xml添加
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
org.apache.commons
commons-lang3
3.7
②application.properties配置文件添加
###################pagehelper分页插件#####################
logging.level.com.example.demo.dao=DEBUG
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=true
③Mapper添加方法
@Select("SELECT * FROM USER")
List list();
④UserServiceImpl添加方法,page当前页,size当前页数量
public List list(Integer page,Integer size){
// 开启分页插件,放在查询语句上面
PageHelper.startPage(page, size);
//查询语句
List userList = userMapper.list();
// 封装分页之后的数据
PageInfo pageInfoUserList = new PageInfo(userList);
//通过getList()获取分页后的数据
return pageInfoUserList.getList();
}
⑤UserController添加代码
@RequestMapping(value = "/list/{page}/{size}",method = RequestMethod.GET)
public List list(@PathVariable Integer page,@PathVariable Integer size){
return userService.list(page,size);
}
⑥运行测试如下
三、整合SpringDataJpa
1、pom.xml添加
org.springframework.boot
spring-boot-starter-data-jpa
2、实体类
package com.caofanqi.entity;
import com.caofanqi.util.StringUtil;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity(name="user")
public class User {
/**
* JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
* TABLE:使用一个特定的数据库表格来保存主键。
* SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
* IDENTITY:主键由数据库自动生成(主要是自动增长型)
* AUTO:主键由程序控制,如果不指定主键生成策略,默认为AUTO
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
private String name;
private Integer age;
/**
* 对应的数据库字段,默认驼峰命名法,如果不一致可以通过@Column的name属性修改
*/
@Column(name = "id_card")
private String idCard;
public User(String name, Integer age) {
this.id = StringUtil.guid();
this.name = name;
this.age = age;
}
}
3、UserDao
package com.caofanqi.dao;
import com.caofanqi.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserDao extends JpaRepository {
/**
* 可以通过@Query,并设置nativeQuery = true,写SQL语句
* 注意:LIMIT 的以一个参数是起始行数 =(当前页-1)* pageSize
*/
@Query(value = "SELECT * FROM user LIMIT ?1,?2",nativeQuery = true)
List list(Integer startRow, Integer pageSize);
}
4、UserServiceImpl添加userDao注入和方法
@Autowired
private UserDao userDao;
public User findById(String id){
Optional opUser = userDao.findById(id);
return opUser.get();
}
5、UserController添加方法
@RequestMapping(value = "/find/{id}",method = RequestMethod.GET)
public User findById(@PathVariable String id){
User user = userService.findById(id);
return user;
}
6、启动运行如下
项目源码:https://gitee.com/itcaofanqi/CaoFanqiStudyRepository/tree/master/stuspringboot/