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>
上一篇:弱口令


下一篇:NETCONF下发网络设备配置:netconf_switch