- 添加pom依赖
<!-- 引入mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySQL的JDBC驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency><!-- 引入第三方数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
- 数据源连接配置
#数据源连接配置
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =root
spring.datasource.password =123456
- 开启mybatis日志打印
#开启mybatis日志打印
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
- Application类添加mapper扫描注解
@MapperScan("com.example.demo.mapper")
- 创建表user
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL COMMENT '名称',
`phone` varchar(16) DEFAULT NULL COMMENT '用户手机号',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`age` int(4) DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
- User实体类
package com.example.demo.entity;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class User {
private long id;
private String name;
private String phone;
private int age;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", locale="zh", timezone="GMT+8")
private Date createTime;public long getId() {
return id;
}public void setId(long 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 int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}public Date getCreateTime() {
return createTime;
}public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
- UserMapper
package com.example.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;import com.example.demo.entity.User;
public interface UserMapper {
@Insert("insert into user(name,phone,age,create_time) values(#{name},#{phone},#{age},#{createTime})")
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
int insert(User user);@Select("select * from user")
@Results({
@Result(column="create_time", property="createTime")
})
List<User> findAll();
@Select("select * from user where id=#{id}")
@Results({
@Result(column="create_time", property="createTime")
})
User findById(long id);
@Update("update user set name=#{name} where id=#{id} ")
void update(User user);
@Delete("delete from user where id=#{id}")
void delete(long id);
}
- UserService
package com.example.demo.service;
import com.example.demo.entity.User;
public interface UserService {
public int add(User user);
}
- UserServiceImpl
package com.example.demo.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;@Service
public class UserServiceImpl implements UserService{@Autowired
private UserMapper userMapper;
@Transactional(propagation=Propagation.REQUIRED)
public long add(User user) {
userMapper.insert(user);
return user.getId();
}}
- UserController
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private UserMapper userMapper;
@PostMapping("/add")
public Object add(@RequestBody User user){
long id = userService.add(user);
return id;
}
@RequestMapping("/findAll")
public Object findAll(){
return userMapper.findAll();
}
@RequestMapping("/findById")
public Object findById(long id){
return userMapper.findById(id);
}
@PostMapping("/update")
public Object update(@RequestBody User user){
userMapper.update(user);
return "update success!";
}
@PostMapping("/delete")
public Object delete(long id){
userMapper.delete(id);
return "delete success!";
}
}