JavaEE之mybatis代理开发模式

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。下面我简单说说怎么创建mybatis的代理开发模式:

第一步:创建一个空的Javaweb项目,这个我就不详细说了;

第二步:导包,这个我也就不详细说了;

第三步:创建Javabean:

package com.fengqi.vo;
import java.util.Date;
public class Users {
	private Integer id;
	private String user_name;
	private Date user_birthday;
	private String user_sex;
	private Integer user_age;
	private String user_addr;
	private String user_iphone;
	public Users() {
		
	}
	public Users(Integer id, String user_name, Date user_birthday,
			String user_sex, Integer user_age, String user_addr,
			String user_iphone) {
		super();
		this.id = id;
		this.user_name = user_name;
		this.user_birthday = user_birthday;
		this.user_sex = user_sex;
		this.user_age = user_age;
		this.user_addr = user_addr;
		this.user_iphone = user_iphone;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public Date getUser_birthday() {
		return user_birthday;
	}
	public void setUser_birthday(Date user_birthday) {
		this.user_birthday = user_birthday;
	}
	public String getUser_sex() {
		return user_sex;
	}
	public void setUser_sex(String user_sex) {
		this.user_sex = user_sex;
	}
	public Integer getUser_age() {
		return user_age;
	}
	public void setUser_age(Integer user_age) {
		this.user_age = user_age;
	}
	public String getUser_addr() {
		return user_addr;
	}
	public void setUser_addr(String user_addr) {
		this.user_addr = user_addr;
	}
	public String getUser_iphone() {
		return user_iphone;
	}
	public void setUser_iphone(String user_iphone) {
		this.user_iphone = user_iphone;
	}
	@Override
	public String toString() {
		return "Users [id=" + id + ", user_name=" + user_name
				+ ", user_birthday=" + user_birthday + ", user_sex=" + user_sex
				+ ", user_age=" + user_age + ", user_addr=" + user_addr
				+ ", user_iphone=" + user_iphone + "]";
	}
}

第四步:创建接口:

package com.fengqi.dao;
import com.fengqi.vo.Users;
public interface UsersMapper {
	//查找用户接口
	public Users selectUser(String id);
	//删除用户接口
	public Users delectUser(int id);
	//插入用户接口
	public Users insertUser(Users user);
	//修改用户接口
	public Users updateUser(Users user);
}

第五步:创建mapper:




	
  
  	
  
  	delete from userss where id=#{id}
  
  	
  
  		insert into userss(id,user_name,user_birthday,user_sex,user_age,user_addr,user_iphone) values(#{id},#{user_name},#{user_birthday},#{user_sex},#{user_age},#{user_addr},#{user_iphone});
  
  	
  
  		update userss set user_name=#{user_name},user_birthday=#{user_birthday},user_sex=#{user_sex},user_age=#{user_age},user_addr=#{user_addr},user_iphone=#{user_iphone}  where id=#{id}
  

第六步:创建全局配置文件:




	
	
		
	
	
	
		
			
			
				
				
				
				
			
		
	
	
	
		   
	

第七步:创建测试类:

package com.fengqi.test;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
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.fengqi.vo.Users;
public class UserTest {
	public static void main(String[] args) throws SQLException, IOException{
		String resource = "configuration.xml";
		//加载配置文件
		Reader reader = Resources.getResourceAsReader(resource);
		//根据read获取SqlSessionFactory 对象
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
		//根据SqlSessionFactory获取SqlSession对象
		SqlSession session = sessionFactory.openSession();
		try{
			//根据ID查找用户
			Users user = (Users)session.selectOne("com.fengqi.dao.UsersMapper.selectUser","3");
			System.out.println("username:"+user.getUser_name());
			//根据ID删除用户
			/*session.delete("com.fengqi.dao.UsersMapper.delectUser",3);
			session.commit();*/
			//插入用户
			/*Users ui = new Users(null,"archybold8",new Date(),"0",11,"陕西省西安市","18392357490");
			session.insert("com.fengqi.dao.UsersMapper.insertUser",ui);
			session.commit();*/
			//更新用户
			/*Users ui = new Users(14,"archy78",new Date(),"1",29,"陕西省西安市","18392357490");
			session.update("com.fengqi.dao.UsersMapper.updateUser", ui);
			session.commit();	*/	
		}finally{
			//关闭session
			session.close();
		}
	}
}
第八步:测试结果:

JavaEE之mybatis代理开发模式_第1张图片

第八步:项目总览:

JavaEE之mybatis代理开发模式_第2张图片

你可能感兴趣的:(JavaEE之mybatis代理开发模式)