8、mybatis之增删改查
生活随笔
收集整理的這篇文章主要介紹了
8、mybatis之增删改查
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1、EmployeeMapper.xml
- 2、測試增刪改查
1、EmployeeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--指定為接口的全類名--> <mapper namespace="com.mi.dao.EmployeeMapper"><!--id 為接口的方法名--><select id="getEmployeeById" resultType="com.mi.pojo.Employee">select * from employee where id = #{id}</select><insert id="addEmp" parameterType="com.mi.pojo.Employee">insert into employee (last_name,gender) values (#{lastName},#{gender})</insert><delete id="deleteEmp">delete from employee where id = #{id}</delete><update id="updateEmp" >update employee set last_name = #{lastName},gender = #{gender} where id = #{id}</update> </mapper>2、測試增刪改查
package com.mi.test;import com.mi.dao.EmployeeMapper; import com.mi.pojo.Employee; 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 org.junit.Test;import java.io.IOException; import java.io.InputStream;public class TestMybatis {public SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(inputStream);}@Testpublic void testGet() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現(xiàn)類對象//會為接口自動創(chuàng)建一個代理對象,代理對象去執(zhí)行增刪改查方法EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Employee employee = mapper.getEmployeeById(3);System.out.println(employee);}finally {sqlSession.close();}}@Testpublic void testAdd() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現(xiàn)類對象//會為接口自動創(chuàng)建一個代理對象,代理對象去執(zhí)行增刪改查方法EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Integer employee = mapper.addEmp(new Employee(null,"jerry","1"));sqlSession.commit();System.out.println(employee);}finally {sqlSession.close();}}@Testpublic void testDel() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現(xiàn)類對象//會為接口自動創(chuàng)建一個代理對象,代理對象去執(zhí)行增刪改查方法EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Integer employee = mapper.deleteEmp(1);sqlSession.commit();System.out.println(employee);}finally {sqlSession.close();}}@Testpublic void testUpdate() throws IOException {//1、獲取SqlSessionFactory對象SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2、獲取Sqlsesion對象SqlSession sqlSession = sqlSessionFactory.openSession();try {//3、獲取接口的實現(xiàn)類對象//會為接口自動創(chuàng)建一個代理對象,代理對象去執(zhí)行增刪改查方法EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Integer employee = mapper.updateEmp(new Employee(3,"jerry","0"));sqlSession.commit();System.out.println(employee);}finally {sqlSession.close();}}}總結(jié)
以上是生活随笔為你收集整理的8、mybatis之增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7、mybatis主配置文件之mappe
- 下一篇: 9、mybatis自增主键策略