一.环境准备
我这里用的是
- jdk1.8
- IntelliJ IDEA
- maven 3.3.9
上面环境安装及配置请自己百度
二 建库、建表
我用的navicat premium工具手动建的表
建表sql语句
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(15) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '',
`password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '',
`address` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
三 编写代码
1. 我的项目结构如下:
- lombok插件安装
3. application.yml配置文件
server:
port: 80
spring:
datasource:
url: jdbc:mysql://localhost:3306/yourDB?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: XXXX
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.zlq.demo.pojo
configuration:
map-underscore-to-camel-case: true
logging:
level:
com.zlq.demo: debug
4. maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zlq.dev</groupId>
<artifactId>mybatis-demo-zlq</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!--添加spring-boot-starter-parent主要是统一版本管理,通常的依赖不用鞋<version></version>-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--不用引入jdbc依赖了,此依赖默认包含spring-boot-starter-jdbc-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--web项目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--省了写getter/setter toString 构造函数和equals hashCode等-->
<!--idea需要装lombok插件,idea插件安装请百度-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!--测试环境-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
5. 实体类 User
//@Data lombok插件带的注解(不用写getter/setter toString等)
//@NoArgsConstructor 无参构造函数
//@AllArgsConstructor
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable{
private String id;
private String name;
private String password;
private String address;
}
6. Dao层 User接口 UserDao
//带上这个注解spring boot启动类上就不用带@MapperScan了
@Mapper
public interface UserDao {
//通过id查询用户
public User get(String id);
//查询所用用户
public List<User> findAll();
//修改用户信息,通过id
public int update(User user);
//增加用户信息
public int insert(User user);
//删除用户
public int delete(String id);
}
7. Service层 UserService
@Service
public class UserService {
@Autowired
private UserDao userDao;
//通过id查询用户
public User get(String id) {
return userDao.get(id);
}
//查询所用用户
public List<User> findAll() {
return userDao.findAll();
}
//增加用户
public int insert(User user) {
return userDao.insert(user);
}
//更新用户
public int update(User user) {
return userDao.update(user);
}
//删除用户
public int delete(String id) {
return userDao.delete(id);
}
}
8. Controller层 UserController
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("{user_id}")
public User getUser(@PathVariable("user_id") String id){
return userService.get(id);
}
@GetMapping
public List<User> findAllUser(){
return userService.findAll();
}
@PostMapping
public int addUser(@RequestBody User user){
return userService.insert(user);
}
@PutMapping
public int updateUser(@RequestBody User user){
return userService.update(user);
}
@DeleteMapping("{id}")
public int deleteUser(@PathVariable("id") String id){
return userService.delete(id);
}
}
9. UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zlq.demo.dao.UserDao">
<select id="get" parameterType="string" resultType="user">
SELECT id,name,password,address
FROM t_user WHERE id=#{id}
</select>
<select id="findAll" resultType="user">
SELECT id,name,password,address FROM t_user
</select>
<update id="update" parameterType="user">
UPDATE t_user
<set>
<if test="name!= null">
name = #{name},
</if>
<if test="password!= null">
password = #{password},
</if>
<if test="address!= null">
address = #{address},
</if>
</set>
WHERE id = #{id}
</update>
<insert id="insert" parameterType="user" useGeneratedKeys="true" keyProperty="id">
INSERT INTO t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null and id != ''">
id,
</if>
<if test="name!= null">
name,
</if>
<if test="password!= null">
password,
</if>
<if test="address!= null">
address,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null and id != ''">
#{id},
</if>
<if test="name!= null">
#{name},
</if>
<if test="password!= null">
#{password},
</if>
<if test="address!= null">
#{address},
</if>
</trim>
</insert>
<delete id="delete" parameterType="string">
DELETE FROM t_user WHERE id=#{id}
</delete>
</mapper>
到这里基础环境和项目已经搞定。
四 项目测试
1. 用idea自带的restful工具测试
2.增加一个用户
成功增加一条数据
其它接口也都能运行,就不一 一测试。
OK!搞定手工!
拜拜!