mybatis实现增删改查
UserDao接口
package dao; import bean.Cloud; import bean.Student; import bean.Teacher; import java.util.List; import java.util.Map; //等价于mapper public interface UserDao { //查询所有信息 List<Cloud> getCloud(); //根据id查询 Cloud queryCloud(int id); //添加 int addUser(Cloud cloud); //修改 int updateById(Cloud cloud); //删除 int delete(int id); //用map int update1(Map<String,Object> map); //模糊查询 List<Cloud> getUserLike(String value); //查询所有学生 List<Student> getStu(); //查询老师 List<Teacher> getTeacher(); }
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"> <!--namespace绑定一个Dao/Mapper接口--> <mapper namespace="dao.UserDao"> <select id="getCloud" resultType="bean.Cloud"> select * from caijing </select> <!-- value是模糊查询的参数--> <select id="getUserLike" resultType="cloud"> select * from caijing where word like "%"#{value}"%" </select> <select id="queryCloud" parameterType="_int" resultType="bean.Cloud"> select * from caijing where id = #{id} </select> <insert id="addUser" parameterType="bean.Cloud"> insert into caijing (id,word,count) values (#{id},#{word},#{count}); </insert> <update id="updateById" parameterType="bean.Cloud"> update caijing set word = #{word},count = #{count} where id=#{id}; </update> <update id="update1" parameterType="map"> update caijing set word = #{words} where id=#{userid}; </update> <delete id="delete" parameterType="int"> delete from caijing where id = #{id}; </delete> <select id="getStu" resultMap="getStuTeacher"> select * from student; </select> <resultMap id="getStuTeacher" type="bean.Student"> <result property="id" column="id"/> <result property="name" column="name"/> <!-- 复杂的属性,我们需要单独处理 对象:association 集合:collection--> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="bean.Teacher"> select * from teacher where id = #{id}; </select> </mapper>
工具类:
package utils; 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 java.io.IOException; import java.io.InputStream; //sqlSessionFactory对象 public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "mybatis-config.xml"; InputStream inputStream = null; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } }
测试类:
package dao; import bean.Cloud; import bean.Student; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import utils.MybatisUtils; import java.util.HashMap; import java.util.List; import java.util.Map; public class UserDaoTest { @Test public void test(){ //1.获取sqlsession对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //2.1执行sql UserDao mapper = sqlSession.getMapper(UserDao.class); List<Cloud> list = mapper.getCloud(); for (Cloud cloud : list) { System.out.println(cloud); } //关闭sqlsession sqlSession.close(); } @Test public void query(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); Cloud cloud = mapper.queryCloud(1000); System.out.println(cloud); sqlSession.close(); } //增删改需要提交事务 @Test public void addUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); Cloud cloud = new Cloud(1001, "天岁", "10"); int i = mapper.addUser(cloud); if (i==1) System.out.println("成功"); else System.out.println("失败"); sqlSession.commit(); //必须提交事务,我以前没写过啊! sqlSession.close(); } @Test public void update(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); Cloud cloud = new Cloud(1001, "天岁", "11"); int i = mapper.updateById(cloud); if (i==1) System.out.println("成功"); else System.out.println("失败"); sqlSession.commit(); //必须提交事务,我以前没写过啊! sqlSession.close(); } @Test public void delete(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); int i = mapper.delete(1001); if (i==1) System.out.println("成功"); else System.out.println("失败"); sqlSession.commit(); sqlSession.close(); } @Test public void updateTest(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); Map<String, Object> map = new HashMap<String,Object>(); map.put("words","在发展"); map.put("userid",1); int i = mapper.update1(map); if (i==1) System.out.println("成功"); else System.out.println("失败"); sqlSession.commit(); sqlSession.close(); } //模糊查询实例 @Test public void getCloudLike(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); List<Cloud> list = mapper.getUserLike("我"); for (Cloud cloud : list) { System.out.println(cloud); } sqlSession.close(); } //********************************* //一对多实例 @Test public void getStu(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); List<Student> stu = mapper.getStu(); for (Student student : stu) { System.out.println(student); } sqlSession.close(); } }