04_resultMap配置

一. resultMap配置

1. resultMap解决实体类和数据库字段名不一致的问题

04_resultMap配置

二. resultMap配置例子

1. 新建一个名为MybatisResultMap的Java工程, 拷入相关jar包

04_resultMap配置

2. 新建User.java

package com.lywgames.domain;

import java.util.Date;

public class User {
	private Integer user_id;
	private String user_name;
	private String sex;
	private Date birthday;
	private String address;

	public User() {
	}
	
	public User(String user_name, String sex, Date birthday, String address) {
		this.user_name = user_name;
		this.sex = sex;
		this.birthday = birthday;
		this.address = address;
	}
	
	public User(Integer user_id, String user_name, String sex, Date birthday, String address) {
		this.user_id = user_id;
		this.user_name = user_name;
		this.sex = sex;
		this.birthday = birthday;
		this.address = address;
	}

	public Integer getUser_id() {
		return user_id;
	}

	public void setUser_id(Integer user_id) {
		this.user_id = user_id;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [user_id=" + user_id + ", user_name=" + user_name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address
				+ "]";
	}

}

3. 新建UserMapper.java接口

package com.lywgames.dao;

import java.util.List;
import com.lywgames.domain.User;

public interface UserMapper {
	public List<User> selectUser(String name);
	public int insertUser(User user);
	public int updateUser(User user);
	public int deleteUser(String name);
}

4. 在和UserMapper.java接口同一目录下新建UserMapper.xml映射文件

04_resultMap配置

5. 在src目录下新建jdbc.properties数据库属性文件

04_resultMap配置

6. 在src目录下新建SqlMapConfig.xml数据库配置文件

04_resultMap配置

7. 新建Test.java

package com.lywgames;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.lywgames.dao.UserMapper;
import com.lywgames.domain.User;

public class Test {
	public static void main(String[] args) {
		SqlSession sqlSession = null;
		try {
			// 指定全局配置文件
			String resource = "SqlMapConfig.xml";
			// 读取配置文件
			InputStream inputStream = Resources.getResourceAsStream(resource);
			// 构建sqlSessionFactory
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			// 获取sqlSession
			sqlSession = sqlSessionFactory.openSession();

			UserMapper userDao = sqlSession.getMapper(UserMapper.class);
			// 插入操作
			User insertUser = new User("李中", "男", new Date(System.currentTimeMillis()), "河南省郑州市");
			int insertResult = userDao.insertUser(insertUser);
			System.out.println("插入操作影响行数:" + insertResult);
			// 查询操作
			List<User> users = userDao.selectUser("李");
			for (User user : users) {
				System.out.println(user);
			}
			// 更新操作
			User user = new User(4, "李苗", "女", null, null);
			int updateResult = userDao.updateUser(user);
			System.out.println("更新操作影响行数:" + updateResult);
			// 删除操作
			int deleteResult = userDao.deleteUser("英");
			System.out.println("删除操作影响行数:" + deleteResult);
			
			// 写操作, 需要提交事务。
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(sqlSession != null) {
				// 释放资源
				sqlSession.close();
			}
		}
	}
}

8. 查看user表数据

04_resultMap配置

9. 运行项目

04_resultMap配置

10. 运行项目后查看user表数据

04_resultMap配置

上一篇:Myabtis中的ResultMap


下一篇:mybatis根据property获取column