Mybatis 用注解处理CRUD

修改config.xml文件:

  <mappers>
    <!-- <mapper resource="com/dw/dao/userMapper.xml"/>  -->
    <mapper class="com.dw.dao.UserMapper"/>
  </mappers>

UserMapper.java:
package com.dw.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.dw.entity.User;

public interface UserMapper {
  @Select("select * from user as u where u.id=#{id}") //数据表中的字段名与实体属性对应可以用这种方法,否则用下一种
  //@Select("select id,t_username as username,password from user as u where u.id = #{id}")
  @ResultType(User.class)  //可以不写
	public User selectById(Integer id);
  
  @Select("select * from user")
  @Results({ //相当于resultMap ,这里也可以不写,当字段名与属性相同时
	  @Result(property="username",column="username"),
	  @Result(property="password",column="password")
  })
  public List<User> selectAll();
  
  //多参数查询
  //@Select("select id,username,password from user as u where u.id=#{0} and u.username=#{1}")
  @Select("select id,username,password from user as u where u.id=#{id} and u.username=#{name}")
  public User selectByMulParam(@Param("id")Integer id,@Param("name")String name);

  
  @Select("select id,username,password from user as u where u.id=#{a} and u.username=#{b}")
  public User selectByMap(Map<String, Object> mp);
  
  
  
  @Update("update user as u set u.password=#{pwd} where u.id=#{id}")
  public void updateById(@Param("id")Integer id,@Param("pwd")String pwd);
  
  @Update("update user as u set u.password=#{password} where u.id=#{id}")
  public void updateByUser(User user);
  
  
  @Insert("insert into user(username,password) values(#{username},#{password})")
  public void insertUser(User user);
  
  @Delete("delete from user where user.username = #{n}")
  public void delectUserByName(String name);
}

测试:

/*
  * 注解配置
  */
 
  @Test
  public void testSelectById(){
	  init();
	  Integer id = 5;
	  User user = userMapper.selectById(id);
	  System.out.println(user);
	  destory();
  }

  @Test
  public void testSelectAll(){
	  init();
	  List<User> list = userMapper.selectAll();
	  for (User user : list) {
		System.out.println(user);
	}
	  destory();
  }  
  
  @Test
  public void testSelectByMulParam(){
	  init();
	  Integer id = 5;
	  String name = "李斯";
	  User user = userMapper.selectByMulParam(id, name);
	  System.out.println(user);
	  destory();
  } 
  
  @Test
  public void testSelectByMap(){
	  init();
	  Map<String, Object> mp = new HashMap<>();
	  mp.put("a", 5);
	  mp.put("b", "李斯");
	  User user = userMapper.selectByMap(mp);
	  System.out.println(user);
	  destory();
  } 
  
  
  @Test
  public void testUpdateById(){
	  init();
      Integer id = 5;
      String pwd = "hahaha";
	  userMapper.updateById(id, pwd);
	  destory();
  }   
  
  @Test
  public void testUpdateByUser(){
	  init();
      User user = new User();
      user.setId(5);
      user.setPassword("heihei");
	  userMapper.updateByUser(user);
	  destory();
  }  
  
  
  @Test
  public void testInsertUser(){
	  init();
      User user = new User();
      user.setUsername("zhoujielun");
      user.setPassword("123456");
	  userMapper.insertUser(user);
	  destory();
  }  
  
  @Test
  public void testDelectUser(){
	  init();
	  userMapper.delectUserByName("zhoujielun");
	  destory();
  }  
  
	private void init(){
		try {
			inputStream = Resources.getResourceAsStream("config.xml");
			sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			session = sessionFactory.openSession();
			//userDao = session.getMapper(UserDao.class);
			userMapper = session.getMapper(UserMapper.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void destory(){
		if(session != null){
			session.close();
		}
		if(inputStream != null){
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}  





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值