MyBatis 数据库操作

2021/9/29 2:10:45

本文主要是介绍MyBatis 数据库操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、po(src/main/java文件下)

package demo.boot.po;

public class User {
	private int id;
	private String uname;
	private String pwd;
	private String nikname;
	private String phone;

	public int getId() {
		return id;
	}

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

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", uname=" + uname + ", pwd=" + pwd + ", nikname=" + nikname + ", phone=" + phone
				+ "]";
	}

	public User() {
		super();
	}

	public User(int id, String uname, String pwd, String nikname, String phone) {
		super();
		this.id = id;
		this.uname = uname;
		this.pwd = pwd;
		this.nikname = nikname;
		this.phone = phone;
	}

	public String getNikname() {
		return nikname;
	}

	public void setNikname(String nikname) {
		this.nikname = nikname;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

}

二、mapper(src/main/java文件下)

package demo.boot.mapper;

import java.util.List;
import java.util.Map;

import demo.boot.po.User;

public interface UserMapper {

	public List<User> selectAll();
	
	public User selectByAuth(Map<String, String> params); //String name, String pwd

	public int insertUser(User user);
	
	public int deleteUserById(int id);
	
	public int updateUser(User user);
}

三、mappers(src/main/resources文件下)

<?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="demo.boot.mapper.UserMapper">
	<select id="selectAll" resultType="User">
		select * from t_user
	</select>
	
	<select id="selectByAuth" parameterType="Map" resultType="User">
		select * from t_user where name=#{name} and password=#{pwd}
	</select>
	
	<insert id="insertUser" parameterType="User">
		insert into t_user values(default, #{name}, #{password})
	</insert>
	
	<delete id="deleteUserById" parameterType="int">
		delete from t_user where id = #{id}
	</delete>
	
	<update id="updateUser" parameterType="User">
		update t_user set name=#{name}, password=#{password} where id=#{id}
	</update>
</mapper>


这篇关于MyBatis 数据库操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程