Mybatis 用注解处理CRUD
来源:互联网 发布:宽带多重网络怎么回事 编辑:程序博客网 时间:2024/05/21 03:25
修改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
- Mybatis 用注解处理CRUD
- mybatis的CRUD(注解版)
- Mybatis之使用注解开发CRUD
- MyBatis的注解实现简单的CRUD
- Mybatis的CRUD操作(基于注解)
- Mybatis之使用注解开发CRUD
- Mybatis之使用注解开发CRUD
- 使用MyBatis(七)使用java注解完成CRUD
- mybatis使用注解编写crud后,不配置mapping方法。
- Mybatis基于注解结合Java编写Sql语句进行CRUD
- Mybatis使用注解的方式CRUD包括批量插入更新
- mybatis CRUD
- mybatis (CRUD)
- 用mybatis实现简单的CRUD
- springmvc--注解crud
- 使用MyBatis对表执行CRUD操作——基于注解的实现
- SpringBoot之Mybatis连接MySQL进行CRUD(注解&配置文件)(简测试版)
- MyBatis实现CRUD
- 线程间的通信、同步方式与进程间通信方式
- RAM 大全-DRAM, SRAM, SDRAM的关系与区别
- 图的宽度优先搜索(BFS)以及C++队列知识复习
- 如何开发数字货币钱包安装使用教程使用教程图解
- S10泛型算法
- Mybatis 用注解处理CRUD
- 如何做项目
- c# ArrayList
- 软件工程五种典型的开发模型各自特点
- Qt获取本机的IP地址、用户名等相关信息
- 加密解密,数字签名及证书
- AndroidStudio(3.x版本)的输入法之坑
- 给定一棵二叉树。求两个树节点的最低公共祖先
- 更改单选按钮的选项值来改变调查内容