04_resultMap配置

一. resultMap配置

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

二. resultMap配置例子

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

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映射文件

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

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

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表数据

9. 运行项目

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值