一、引言
二、操作
1、包结构如图
2、mapper配置
<?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.bysoft.springmybatismanual.dao.UserDao">
<resultMap id="BaseResultMap" type="com.bysoft.springmybatismanual.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, user_name, password, age
</sql>
<select id="findAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user_t
</select>
<insert id="addUser" parameterType="com.bysoft.springmybatismanual.entity.User">
insert into user_t (user_name, password, age)
values (#{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
</insert>
<update id="updateUser" parameterType="com.bysoft.springmybatismanual.entity.User" >
update user_t
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<delete id="deleteUser" parameterType="java.lang.Integer" >
delete from user_t
where id = #{id,jdbcType=INTEGER}
</delete>
</mapper>
3、User
package com.bysoft.springmybatismanual.entity;
public class User {
private Integer id;
private String userName;
private String password;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null? null:userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null? null:password.trim() ;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
4、UserDao
@Repository
public interface UserDao {
/**
* 查询所有用户
* @return
*/
List<User> findAll();
/**
* 添加用户
* @param user
*/
void addUser(User user);
/**
* 跟新用户
* @param user
*/
void updateUser(User user);
/**
* 删除用户
* @param id
* @return
*/
int deleteUser(Integer id);
}
5、UserService
public interface UserService {
/**
* 查询所有用户
* @return
*/
List<User> findAll();
/**
* 添加用户
* @param user
*/
void addUser(User user);
/**
* 跟新用户
* @param user
*/
void updateUser(User user);
/**
* 删除用户
* @param id
* @return
*/
int deleteUser(Integer id);
}
6、UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void addUser(User user) {
userDao.addUser(user);
}
@Override
public void updateUser(User user) {
userDao.updateUser(user);
}
@Override
public int deleteUser(Integer id) {
return userDao.deleteUser(id);
}
}
7、UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "/showUser")
public List<User> getUser(){
return userService.findAll();
}
@PostMapping(value = "/addUser")
public void addUser(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("age") Integer age){
User user = new User();
user.setUserName(username);
user.setPassword(password);
user.setAge(age);
userService.addUser(user);
}
@PutMapping(value = "/updateUser/{id}")
public void updateUser(@PathVariable("id") Integer id,
@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("age") Integer age){
User user = new User();
user.setId(id);
user.setUserName(username);
user.setPassword(password);
user.setAge(age);
userService.updateUser(user);
}
@DeleteMapping("/deleteUser/{id}")
public void deleteUser(@PathVariable("id") Integer id){
userService.deleteUser(id);
}
}
8、application.yml
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3308/user_test?useUnicode=true&useSSL=false&characterEncoding=UTF-8
server:
port: 9090
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.bysoft.springmybatismanual.entity
三、总结
注意事项:
1、启动类的包扫描一定要扫描上 实体类所在的包
2、mybatis要配置映射文件的位置 mapper-locations