dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
application.yml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/PersonManage?useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.flash.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
UserController
package com.flash.controller;
import com.flash.mapper.UserMapper;
import com.flash.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
@GetMapping("/quarryUserList")
public List<User> quarryUserList(){
List<User> users = userMapper.quarryUserList();
for(User user :users)
System.out.println(user);
return users;
}
@GetMapping("/updateUser")
public String updateUser(User user){
userMapper.updateUser(new User(5,"abc123","123","123"));
return "0K!";
}
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new User(12,"xiaowang","12312","321"));
return "OK";
}
@GetMapping("/addUser2")
public String addUser2(Map<Object,Object> object){
HashMap<String, Object> map = new HashMap<>();
map.put("name","asdad");
map.put("account","3321");
map.put("password","1212d");
userMapper.addUser2(map);
return "OK!";
}
}
UserMapper
package com.flash.mapper;
import com.flash.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Mapper//这个注解表示是mybatis的一个mapper类
@Repository
public interface UserMapper {
List<User> quarryUserList();
User quarryUserListById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
void addUser2(Map<String, Object>map);
}
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.flash.mapper.UserMapper">
<select id="quarryUserList" resultType="com.flash.pojo.User">
select * from PersonManage.admin
</select>
<select id="quarryUserListById" resultType="com.flash.pojo.User">
select * from admin where uid = #{id}
</select>
<insert id="addUser" parameterType="User">
insert into admin(uid,uname,account,password) values (#{uid},#{uname},#{account},#{password})
</insert>
<update id="updateUser" parameterType="User">
update admin set uname = #{uname},password = #{password},account=#{account} where uid = #{uid}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where uid = #{uid}
</delete>
<select id="addUser2" parameterType="Map">
insert into admin(uname,account,password) values (#{name},#{account},#{password});
</select>
</mapper>