135-1821-9792

mybatis使用xml进行增删改查代码解析

MyBatis是支持普通sql查询、存储过程和高级映射的持久层框架。

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、成都网站制作、兴隆台网络推广、小程序定制开发、兴隆台网络营销、兴隆台企业策划、兴隆台品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供兴隆台建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。

MyBatis可以使用 简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects 普通的Java对象)映射成数据库中的记录。

每一个Mybatis应用程序都以一个sqlSessionFactory对象的实例为核心。

sqlSessionFactory对象的实例可以通过sqlSessionFactoryBuilder对象来获得。sqlSessionFactoryBuilder对象可以通过xml配置文件,或从以往使用管理中准备好的Configuration类实例中来构建sqlSessionFactory对象。

【示例:使用配置类获取sqlSessionFactory】

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
//环境
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
//映射器类
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

注意这种情况下配置是添加映射器类。映射器类是Java类,这些类包含SQL映射语句的注解从而避免了xml文件的依赖,但是xml映射仍然在 大多数高级映射(比如:嵌套join映射)时需要。

出于这样的原因,如果存在xml配置文件的话,MyBatis将会自动查找和加载一个对等的XML文件(这种情况下,基于类路径下的BlogMapper.class类的类名,那么BlogMapper.xml将会被加载–即class 与 XML在同一个文件目录下。如果非,则需要手动配置加载xml)。

【1】基本增删改查xml配置

<?xml version="1.0" encoding="UTF-8" ?>




     
   
    
    
    
   

   
   

   
    delete from t_user where id=#{id}
   


   
    update t_user set username=#{name},age=#{age} where id=#{id}
   

   
    insert into t_user(username,age) values(#{name},#{age})
   

   

   

注册到mybatis.xml [当与spring结合时,将不需要这个配置文件]

mybatis的配置文件

<?xml version="1.0" encoding="UTF-8"?>



  

  
  
    
    
  

  
    
      
      
        
        
        
        
      
    
  

  
    
    
    
  

【2】通过SqlSessionFactory拿到session

这里使用xml文件获取sqlSessionFactory和sqlSession。

public static SqlSessionFactory getFactory(){
	/* flow the src dir*/
	String resource = "mybatis.xml";
	/*MybatisUtils.class.getResourceAsStream(resource)----- it's wrong !!!!
     * please distinguish the two up and down 
     * */
	InputStream inputStream = MybatisUtils.class.getClassLoader().getResourceAsStream(resource);
	SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
	return factory;
}
SqlSession session = factory.openSession(true);
//默认手动提交;
/*
  两种解决方式:
  1.factory.opensession(true);
  2.session.commit();
  */

【3】增删改查后台测试代码

/*use sql xml not annotation*/
@Test
  public void testAdd(){
	SqlSession session = MybatisUtils.getFactory().openSession();
	String statement = "com.web.mapper.userMapper.insertUser";
	/*return the effect rows*/
	int insert= session.insert(statement, new User("tom5", 15));
	/*default is not auto commit*/
	session.commit(true);
	session.close();
	System.out.println("effect rows.."+insert);
}
@Test
  public void testSelect(){
	/*set auto commit ,which equals to the above*/
	SqlSession session = MybatisUtils.getFactory().openSession(true);
	String statement = "com.web.mapper.userMapper.getUser";
	/*return the effect rows*/
	User user = session.selectOne(statement, 3);
	System.out.println("effect rows.."+user);
}
@Test
  public void testUpdate(){
	SqlSession session = MybatisUtils.getFactory().openSession(true);
	String statement = "com.web.mapper.userMapper.updateUser";
	/*return the effect rows*/
	int update= session.update(statement, new User(3,"tom4", 13));
	System.out.println("effect rows.."+update);
}
@Test
  public void testDelete(){
	SqlSession session = MybatisUtils.getFactory().openSession();
	String statement = "com.web.mapper.userMapper.deleteUser";
	/*return the effect rows*/
	int delete= session.delete(statement, 6);
	/* commit by yourself*/
	session.commit();
	System.out.println("effect rows.."+delete);
	session.close();
}
@Test
  public void testGetUsers(){
	SqlSession session = MybatisUtils.getFactory().openSession();
	String statement = "com.web.mapper.userMapper.getUsers";
	/*return the List*/
	List users= session.selectList(statement);
	session.commit();
	System.out.println("effect rows.."+users);
	session.close();
}

Tips :

parameterType 和 resultType 为 hashmap :

  • mapper.xml :
  • test code :
@Test
  public void getUserForMap(){
	SqlSession session = MybatisUtils.getFactory().openSession();
	String statement = "com.web.mapper.userMapper.getUserForMap";
	HashMap map = new HashMap();
	map.put("id", 1);
	/*return the effect rows*/
	Object selectOne = session.selectOne(statement, map);
	/*default is not auto commit*/
	session.commit(true);
	session.close();
	System.out.println("effect rows.."+selectOne+" ,class :"+selectOne.getClass());
}
  • result as follows :
effect rows..{id=1, age=12, name=luli} ,class :class java.util.HashMap

综上可知:mybatis 会根据参数类型和结果类型,自动进行解析封装。

【扩展 基本方法】

【1】分页列表

 

【2】删除方法–根据对象或者Id

如果参数为pojo,mybatis会自动从对象里面获取id ;

  
    delete from tb_user
    where
    id = #{id} 
  

  
    delete from tb_user
    where
    id = #{id} 
  

【3】根据 id list 删除数据

  
    delete from tb_user
    where id in
    --使用foreach
     #{id} 
    
  

【4】getRows

通常与getListPage联合使用。

总结

以上就是本文关于mybatis使用xml进行增删改查代码解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!


网站标题:mybatis使用xml进行增删改查代码解析
本文地址:http://kswsj.com/article/gdshpg.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号