首页 > 代码库 > Mybatis学习笔记-CURD(基于配置文件的方式)
Mybatis学习笔记-CURD(基于配置文件的方式)
User.java实体类
public class User { private int id; private String username; private int age; //... }
userMapper.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.mybatis.test02.userMapper"> <!-- CRUD操作 --> <insert id="addUser" parameterType="com.mybatis.test02.User"> insert into users(username, age) values(#{username}, #{age}) </insert> <delete id="deleteUser" parameterType="int"> delete from users where id = #{id} </delete> <update id="updateUser" parameterType="com.mybatis.test02.User"> update users set username = #{username}, age = #{age} where id = #{id} </update> <select id="getUser" parameterType="int" resultType="com.mybatis.test02.User"> select * from users where id=#{id} </select> <select id="getAllUsers" resultType="com.mybatis.test02.User"> select * from users </select> </mapper>
MybatisUtil.java
public class MybatisUtil { public static SqlSessionFactory getFactory() { String resource = "conf.xml"; InputStream inputStream = Test.class.getClassLoader() .getResourceAsStream(resource); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); return factory; } }
测试方法
@Test public void testAdd() { SqlSessionFactory factory = MybatisUtil.getFactory(); SqlSession session = factory.openSession(true);//设置成自动提交 User user2 = new User(); user2.setUsername("ssdd"); user2.setAge(22); String s = "com.mybatis.test02.userMapper.addUser"; int insert = session.insert(s, user2);//受影响记录的条数 //session.commit(); System.out.println(insert); session.close(); } @Test public void testDelete() { SqlSessionFactory factory = MybatisUtil.getFactory(); SqlSession session = factory.openSession(true);//设置成自动提交 String s = "com.mybatis.test02.userMapper.deleteUser"; int delete = session.delete(s, 1); //session.commit(); System.out.println(delete); session.close(); } //updateUser @Test public void testUpdate() { SqlSessionFactory factory = MybatisUtil.getFactory(); SqlSession session = factory.openSession(true); String s = "com.mybatis.test02.userMapper.updateUser"; User user = new User(); user.setId(3); user.setUsername("umgsai"); user.setAge(24); int update = session.update(s, user); System.out.println(update); session.close(); } @Test public void testSelect(){ SqlSessionFactory factory = MybatisUtil.getFactory(); SqlSession session = factory.openSession(true); String s = "com.mybatis.test02.userMapper.getUser"; User user = session.selectOne(s, 5); System.out.println(user); session.close(); } @Test public void testSelectAll() { SqlSessionFactory factory = MybatisUtil.getFactory(); SqlSession session = factory.openSession(true); String s = "com.mybatis.test02.userMapper.getAllUsers"; List<User>users = session.selectList(s); session.close(); System.out.println(users); }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1559082
Mybatis学习笔记-CURD(基于配置文件的方式)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。