resultMap 简单使用
1、建数据库
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(64) DEFAULT NULL,
`userPass` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
)
2、建model类
public class User implements Serializable{
private Integer id;
private String userName;
private String userPass;
public User() {
super();
}
public User(Integer id, String userName, String userPass) {
this.id = id;
this.userName = userName;
this.userPass = userPass;
}
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;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
}
3、基于接口
1)、mpper 文件
<?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">
<!-- namespace 对应接口名称 -->
<mapper namespace="models.IUser">
<resultMap type="User" id="user">
<id property="id" column="id"/>
<result property="userName" column="userName"/>
<result property="userPass" column="userPass"/>
</resultMap>
<!-- id 对应接口方法名 -->
<insert id="insert" parameterType="user">
INSERT INTO User(userName,userPass) VALUES(#{userName},#{userPass})
</insert>
<delete id="delete" parameterType="models.User">
DELETE FROM User WHERE id=#{id}
</delete>
<update id="update" parameterType="user">
UPDATE User u SET u.userName=#{userName} WHERE u.id=#{id}
</update>
<select id="selectUser" parameterType="integer" resultType="user">
select * from User u where u.id=#{id}
</select>
<select id="selectList" resultType="user">
select * from User u
</select>
</mapper>
2)、测试
package rest;
public class SSs {
static SqlSessionFactory sqlSessionFactory;
static{
String resource = "mybaties.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//添加
private void insertForImpl(){
SqlSession session = sqlSessionFactory.openSession();
IUser iUser = session.getMapper(IUser.class);
User user =new User();
user.setUserName("adadfadasdfasdf");
user.setUserPass("asdfasdfasd");
iUser.insert(user);
session.commit();
}
private void deleteForImpl(){
SqlSession session = sqlSessionFactory.openSession();
IUser iUser = session.getMapper(IUser.class);
User user =new User();
user.setId(89);
iUser.delete(user);
session.commit();
}
private void selectOneForImpl(){
SqlSession session = sqlSessionFactory.openSession();
User user2 = session.selectOne("models.User.sel",1);
System.out.println(user2.getUserName());
}
private void selectListForImpl(){
SqlSession session = sqlSessionFactory.openSession();
IUser iUser = session.getMapper(IUser.class);
List<User> user = iUser.selectList();
for (User user2 : user) {
System.out.println(user2.getUserName());
}
session.commit();
}
public void updateForImpl(){
SqlSession session = sqlSessionFactory.openSession();
IUser iUser = session.getMapper(IUser.class);
User user = iUser.selectUser(90);
System.out.println(user.getUserName());
session.commit();
}
public static void main(String[] args) {
try {
SqlSession session = sqlSessionFactory.openSession();
IUser iUser = session.getMapper(IUser.class);
List<User> user = iUser.selectList();
for (User user2 : user) {
System.out.println(user2.getUserName());
}
session.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、改进配置文件
<update id="update" parameterType="models.User">
UPDATE User u SET u.userName=#{userName} WHERE u.id=#{id}
</update>
1)、 parameterType="models.User" 值为类全路径
2)、在config文件加上
<typeAliases>
<typeAlias alias="User" type="models.User"/>
</typeAliases>
3)、 parameterType="models.User" 改为 parameterType="User"
resultMap 简单使用
原创
©著作权归作者所有:来自51CTO博客作者向往宇宙的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章