springboot整合mybatis
项目创建
包结构
在pom.xml添加阿里连接池和
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties
###springboot 整合mybatis的基础设置
#别名
mybatis.type-aliases-package=cn.hp.springboot04.domain
#驱动配置可省略,springboot可以自动识别
#spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_mybatis
spring.datasource.username =root
spring.datasource.password =root
#如果不配置阿里的druid,会自动使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
#控制台显示SQL
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
Controller层
package cn.hp.springboot04.controller;
import cn.hp.springboot04.domain.User;
import cn.hp.springboot04.mapper.UserMapper;
import cn.hp.springboot04.service.UserService;
import cn.hp.springboot04.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired()
private UserService userService;
@Autowired
private UserMapper userMapper;
@GetMapping("/findall")
public Object findAll() {
List<User> list = userMapper.findAll();
return JsonData.buildSuccess(list);
}
@GetMapping("/add")
public Object add(User user) {
return JsonData.buildSuccess(userService.add(user));
}
@PostMapping("/update")
public Object update(User user) {
System.out.println("更改信息");
userService.update(user);
return JsonData.buildSuccess();
}
@GetMapping("/del_by_id")
public Object delete(Integer id) {
userService.delete(id);
return JsonData.buildSuccess();
}
@GetMapping("/find_by_Id")
public Object findById(Integer id) {
User user = userMapper.findById(id);
return JsonData.buildSuccess(user);
}
@GetMapping("/trans")
public Object testTrans(User user){
userService.useTrans(user);
return JsonData.buildSuccess();
}
}
Mapper
import cn.hp.springboot04.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface UserMapper {
/**
* 查询用户信息
* @return
*/
@Select("select * from user")
@Results(@Result(property = "createTime",column = "create_time"))
public List<User> findAll();
/**
* 根据id查询用户信息
* @param id
* @return
*/
@Select("select * from user where id =#{id}")
@Results(@Result(property = "createTime",column = "create_time"))
User findById(Integer id);
/**
* 根据id更新用户
* @param user
*/
@Update("update user set name = #{name} where id = #{id}")
void update(User user);
/**
* 根据id删除信息
* @param id
*/
@Delete("delete from user where id =#{id}")
void delete(Integer id);
/**
* 添加用户
* @param user
*/
@Insert("insert into user(name,age,phone,create_time) values(#{name},#{age},#{phone},#{createTime})")
@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
int add(User user);
}
Service层
import cn.hp.springboot04.domain.User;
import java.util.List;
public interface UserService {
/**
* 查询用户信息
* @return
*/
public List<User> findAll();
/**
* 根据id查询用户信息
* @param id
* @return
*/
User findById(Integer id);
/**
* 根据id更新用户
* @param user
*/
void update(User user);
/**
* 根据id删除信息
* @param id
*/
void delete(Integer id);
/**
* 添加用户
* @param user
*/
int add(User user);
}
ServiceImpl
import cn.hp.springboot04.domain.User;
import cn.hp.springboot04.mapper.UserMapper;
import cn.hp.springboot04.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImp implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public User findById(Integer id) {
return userMapper.findById(id);
}
@Override
public void update(User user) { userMapper.update(user); }
@Override
public void delete(Integer id) {
userMapper.delete(id);
}
@Override
public int add(User user) {
return userMapper.add(user);
}
}
User
import java.util.Date;
public class User {
private Integer id;
private String name;
private String phone;
private Integer age;
private Date createTime;
public User(Integer id, String name, String phone, Integer age, Date createTime) {
this.id = id;
this.name = name;
this.phone = phone;
this.age = age;
this.createTime = createTime;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
最后在Springboot_mybatisApplication添加@MapperScan(basePackages{“cn.hp.springboot_mybatis.mapper”})注解