mybatis基础教程之二:接口的方式编程分析

不点 阅读:187 2021-03-31 23:21:27 评论:0



                        前面一节,已经搭建好了myeclipse,mybatis,mysql的环境,并且实现了一个简单的查询。请注意,

这种方式是用SqlSession实例来直接执行已映射的SQL语句:
session.selectOne("com.yihaomen.mybatis.models.UserMapper.selectUserByID", 1).其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如UserInterface.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.

下面是详细过程:
1、在源码目录下建立com.mybaties.entry这个包,并建立接口类 UserInterface和实体类User , 内容如下:
实体类:User.class

package com.mybaties.entry; 
 
import java.io.Serializable; 
 
public class User implements Serializable { 
 
	/** 
	 *  
	 */ 
	private static final long serialVersionUID = 1L; 
	 
	private String id; 
	private String username; 
	private String password; 
	private String name; 
	private String sex; 
	private String email; 
	private String company_name; 
	private String phone; 
	private String post_city; 
	private String post_area; 
	private String post_add; 
	private String relation_person; 
	private String relation_phone; 
	 
	public String getId() { 
		return id; 
	} 
	public void setId(String id) { 
		this.id = id; 
	} 
	public String getUsername() { 
		return username; 
	} 
	public void setUsername(String username) { 
		this.username = username; 
	} 
	public String getPassword() { 
		return password; 
	} 
	public void setPassword(String password) { 
		this.password = password; 
	} 
	public String getName() { 
		return name; 
	} 
	public void setName(String name) { 
		this.name = name; 
	} 
	public String getSex() { 
		return sex; 
	} 
	public void setSex(String sex) { 
		this.sex = sex; 
	} 
	public String getEmail() { 
		return email; 
	} 
	public void setEmail(String email) { 
		this.email = email; 
	} 
	public String getCompany_name() { 
		return company_name; 
	} 
	public void setCompany_name(String company_name) { 
		this.company_name = company_name; 
	} 
	public String getPhone() { 
		return phone; 
	} 
	public void setPhone(String phone) { 
		this.phone = phone; 
	} 
	public String getPost_city() { 
		return post_city; 
	} 
	public void setPost_city(String post_city) { 
		this.post_city = post_city; 
	} 
	public String getPost_area() { 
		return post_area; 
	} 
	public void setPost_area(String post_area) { 
		this.post_area = post_area; 
	} 
	public String getPost_add() { 
		return post_add; 
	} 
	public void setPost_add(String post_add) { 
		this.post_add = post_add; 
	} 
	public String getRelation_person() { 
		return relation_person; 
	} 
	public void setRelation_person(String relation_person) { 
		this.relation_person = relation_person; 
	} 
	public String getRelation_phone() { 
		return relation_phone; 
	} 
	public void setRelation_phone(String relation_phone) { 
		this.relation_phone = relation_phone; 
	} 
	 
	 
	 
 
} 


接口类:UserInterface.class

package com.mybaties.entry; 
 
import java.util.List; 
 
 
 
//User 接口定义 
public interface UserInterface { 
	 
    
	    public int insert(User user); 
	     
	    public int update(User user); 
	     
	    public int delete(String userName); 
	     
	    public List<User> selectAll(); 
	     
	    public int countAll(); 
	     
	    public User findByUserName(String userName); 
 
 
}


请注意,这里面有一个方法名insert()、update()、deleted()等方法 必须与 User.xml 里面配置的 select 的id 对应(<select id="insert")等。


User.xml文件

<?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"> 
   
 <mapper namespace="com.mybaties.entry.UserInterface"> 
  
      
      
     <select id="countAll" resultType="int"> 
        select count(*) c from user; 
    </select> 
     
    <select id="selectAll" resultType="com.mybaties.entry.User"> 
        select * from user; 
    </select> 
     
    <insert id="insert" parameterType="com.mybaties.entry.User"> 
        insert into user(id,name,password,sex,email,company_name,phone,post_city,post_area,post_add,relation_person,relation_phone) values(#{id},#{name},#{password},#{sex},#{email},#{company_name},#{phone},#{post_city},#{post_area},#{post_add},#{relation_person},#{relation_phone}) 
    </insert> 
     
    <update id="update" parameterType="com.mybaties.entry.User"> 
        update user set password=#{password},name=#{name} where id=#{id}; 
    </update> 
     
    <delete id="delete" parameterType="String"> 
        delete from user where id=#{id}; 
    </delete> 
     
    <select id="findByUserName" parameterType="String" resultType="com.mybaties.entry.User"> 
        select * from user where id=#{id}; 
    </select> 
      
      
      
 </mapper>



请朋友千万注意:User.xml 中<mapper>工作空间的配置:com.mybaties.entry.UserInterface---指向UserInterface接口,如果工作空间配置的不是这个路径的话,在运行代码时:提示UserInterface接口没有在Configuration.xml 文件中配置。


测试代码:

package com.mybaties.test; 
 
import java.io.IOException; 
import java.io.Reader; 
import java.util.Iterator; 
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.mybaties.entry.User; 
import com.mybaties.entry.UserInterface; 
 
public class Test { 
	 
	/** 
	 * @param 测试方法 
	 */ 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		 SqlSession session = null; 
		try { 
		    String resource = "Configuration.xml"; 
	        Reader reader= Resources.getResourceAsReader(resource); 
			 
			 
				 
	        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); 
	        SqlSessionFactory factory = builder.build(reader);	         
	        session = factory.openSession(); 
	         
	        UserInterface user=session.getMapper(UserInterface.class); 
	        
        
	        //执行User 插入操作 
	        User u=new User(); 
	        u.setCompany_name("4"); 
	        u.setEmail("4"); 
	        u.setId("4"); 
	        u.setPassword("4"); 
	        u.setPhone("4"); 
	        u.setPost_add("4"); 
	        u.setPost_area("4"); 
	        u.setPost_city("4"); 
	        u.setRelation_person("4"); 
	        u.setRelation_phone("4"); 
	        u.setSex("4"); 
	        u.setName("4");	         
	        user.insert(u); 
	        System.out.println("记录条数:"+user.countAll()); 
	        //执行User 查询操作 
	        List<User> users = user.selectAll(); 
	        Iterator<User> iter = users.iterator(); 
	        while(iter.hasNext()){ 
	            User us = iter.next(); 
	            System.out.println("用户名:"+us.getUsername()+"密码:"+us.getPassword()); 
	        } 
	        //执行User 数据更改 
	        u.setName("张三"); 
	        u.setPassword("123456789"); 
	         
	        user.update(u);         
	      /*  User user = (User) session.selectOne("com.mybaties.entry.UserMapper.selectUserByID",1); 
	        System.out.println(user.getName()); 
	        System.out.println(user.getPassword());*/ 
		} catch (IOException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
		 
		 session.commit(); 
		 session.close(); 
 
	}

效果截图:


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号