JavaWeb——Mybatis进阶mapper代理


一、引言



前篇的文章中没有使用mapper代理的方式,调用方法比较麻烦,造成很多代码重复,所以mybatis在输入输出映射的基础上又添加了mapper代理的配置方式方便使用。

先上张图,这张图是说明mybatis发展的进程,本文使用了描红的方法,并没有和spring整合==


JavaWeb——Mybatis进阶mapper代理_第1张图片



二、代码


代码结构如下:


JavaWeb——Mybatis进阶mapper代理_第2张图片


1.配置文件:


SqlMapConfig.xml

这里使用了mapper包自动搜索(此时mapper的java类与xml必须名字相同,在同一个文件夹),别名自动定义==

推荐这么使用,虽然要求多

  


	
	
	
	
		
	
	
	
		
			
			
				
				
				
				
			
		
	
	
	
		
	

log4j.properties

# Global logging configuration 
log4j.rootLogger=DEBUG, stdout 
# MyBatis logging configuration... 
log4j.logger.org.mybatis.example.BlogMapper=TRACE 
# Console output... 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/mybatis
jdbc.username=root
jdbc.password=123

2.mapper中文件:


这两个文件要在同一个文件夹(mapper搜索文件夹),同时命名必须一致==

UserMapper.xml

 


	
		
		
		
	

	
	
	
	
	


UserMapper.java

package cn.xcy.mybatis.mapper;

import java.util.List;

import cn.xcy.mybatis.po.User;
import cn.xcy.mybatis.po.UserCustom;
import cn.xcy.mybatis.po.UserVo;

public interface UserMapper {

	public User findUserByID(int id) throws Exception;
	
	public List findUsers() throws Exception;
	
	public List findUsersByName(String name) throws Exception;

	public List findUserList(UserVo userVo) throws Exception;
	
	public User findUserByIDResultmap(int id) throws Exception;
	
}

3.po文件:


User.java

package cn.xcy.mybatis.po;

public class User {

	private int id;
	private String name;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
}


UserCustom.java

package cn.xcy.mybatis.po;

public class UserCustom extends User {

}


UserVo.java

package cn.xcy.mybatis.po;

public class UserVo {

	private UserCustom userCustom;

	public UserCustom getUserCustom() {
		return userCustom;
	}

	public void setUserCustom(UserCustom userCustom) {
		this.userCustom = userCustom;
	}
	
	
}


4.测试:


		String resource="SqlMapConfig.xml";
		InputStream inputStream= Resources.getResourceAsStream(resource);
		//创建sessionfactory
		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession sqlSession =sqlSessionFactory.openSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		User user=userMapper.findUserByID(2);
		
		List users=userMapper.findUsers();
		
		List listUsers=userMapper.findUsersByName("谢");
		
		UserVo userVo=new UserVo();
		UserCustom userCustom=new UserCustom();
		userCustom.setId(1);
		userCustom.setAge(26);
		userVo.setUserCustom(userCustom);
		List listUsers2=userMapper.findUserList(userVo);
		
		User user1=userMapper.findUserByIDResultmap(3);
		
		sqlSession.close();
		System.out.println(user);


三、总结


  • mapper代理方法的提出

  • mapper代理方法的实现

你可能感兴趣的:(总结,JavaWeb,JavaWeb)