SpringBoot+mybatis增改删查例子

本章节主要介绍springboot+mybatis增改删查用户的完整例子,创建springboot项目可以看Eclipse搭建springboot项目(插件搭建,项目导入搭建),集成mybatis,设置数据源可以看SpringBoot集成mybatis+Druid

1. 创建数据库,表

创建一个数据表t_user  字段为id,name,password,number四个字段,id为int类型,其他的都为String类型。代码生成器会自动生成UserMapper.java,User.java,UserMapper.xml3个文件。

2. User.java实体类

public class User {
	private int id;
	private String name;
	private String password;
	private String number;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;  
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
}

3. Mapper操作增改删查

/*
UserMapper.java
*/
@Mapper
public interface UserMapper {
	//查找
	ListfindUserByName(String name);
	//查找
	public ListListUser();
	//新增
	public int insertUser(User user);
	//删除
	public int delete(int id);
	//修改
	public int Update(User user);
}
/*
UserMapper.xml
*/
<mapper namespace="com.example.demo.mapper.UserMapper">
	<resultMap id="result" type="com.example.demo.entity.User">
		<result property="name" column="name" />
		<result property="password" column="password" />
		<result property="number" column="number"/>
	</resultMap>

	<select id="ListUser" resultMap="result">
		SELECT * FROM user//查找所有user
	</select>

	<select id="findUserByName" resultMap="result">
		SELECT * FROM user where name=#{name}//根据name查找user
	</select>

	<insert id="insertUser" parameterType="com.example.demo.entity.User"
		keyProperty="id" useGeneratedKeys="true">
		INSERT INTO user
		(
		id,name,password,number
		)
		VALUES (
		#{id},
		#{name, jdbcType=VARCHAR},
		#{password, jdbcType=VARCHAR},
		#{number}
		)//添加user
	</insert>
	
	<delete id="delete" parameterType="int">
		delete from user where id=#{id}//根据ID删除user
	</delete>
	
	<update id="Update" parameterType="com.example.demo.entity.User">
	update user set user.name=#{name},user.password=#{password},user.number=#{number} where user.id=#{id}//根据ID修改user
	</update>
</mapper>

UserMapper.java映射UserMapper.xml

4. 业务处理Userservice类

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
        //通过name查找用户
	public ListfindByName(String name) {
		return userMapper.findUserByName(name);
	}
        //添加用户
	public User insertUser(User user) {
		userMapper.insertUser(user);
		return user;
	}
        //查找所有用户
	public ListListUser(){
		return	userMapper.ListUser();
	}
	//更新用户信息
	public int Update(User user){
		return userMapper.Update(user);
	}
	//删除用户信息
	public int delete(int id){
		return userMapper.delete(id);
	}
}

业务处理service类,里面写业务实现代码,这个是不会自动生成的,需要手工创建。

5. web访问controller类

@RestController
@RequestMapping(value = "/CRUD", method = { RequestMethod.GET, RequestMethod.POST })
public class CRUD {
	@Autowired
	private UserService userservice;

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(int id) {
		int result = userservice.delete(id);
		if (result >= 1) {
			return "删除成功";
		} else {
			return "删除失败";
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(User user) {
		int result = userservice.Update(user);
		if (result >= 1) {
			return "修改成功";
		} else {
			return "修改失败";
		}
	}
	
	//添加用户
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public User insert(User user) {
		return userservice.insertUser(user);
	}
	
	//查找所有用户
	@RequestMapping("/ListUser")
	@ResponseBody
	public ListListUser(){
		return userservice.ListUser();
	}

        //查找name相同的所有用户
	@RequestMapping("/ListUserByname")
	@ResponseBody
	public ListListUserByname(String name){
		return userservice.findByName(name);
	}
}

项目结构图如下:

6. 运行springboot

http://localhost:8080/CRUD/ListUser //展示所有用户

http://localhost:8080/CRUD/ListUserByname?name=小王 //查找“小王”的所有用户

http://localhost:8080/CRUD/delete?id=1 //删除id=1的用户


至此SpringBoot+mybatis增改删查功能就实现了,user.java,usermapper.java,usermapper.xml这3个自动生成的代码文件会根据生成器版本不同生成不同的代码,但是就是基本的增改删查功能实现。

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。