本次的环境与上次相同不再过多讲述,实验目录:
本次的insert update delete都是需要在CustomerMapper.xml中继续配置xml标签就可以完成
CustomerMapper.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.zyk.mapper.CustomerMapper"> 5 <select id="findCustomerById" parameterType="Int" resultType="com.zyk.po.Customer"> 6 select * from t_customer where id = #{id} 7 </select> 8 <select id="findCustomerByName" parameterType="String" resultType="com.zyk.po.Customer"> 9 select * from t_customer where username like concat('%',#{value},'%'); 10 </select> 11 <insert id="addCustomer" parameterType="com.zyk.po.Customer"> 12 insert into t_customer(username,jobs,phone) 13 values(#{username},#{jobs},#{phone}) 14 </insert> 15 <delete id="deleteCustomer" parameterType="int"> 16 delete from t_customer where id=#{id} 17 </delete> 18 <update id="updateCustomer" parameterType="com.zyk.po.Customer"> 19 update t_customer set 20 username=#{username},jobs=#{jobs},phone=#{phone} 21 where id=#{id} 22 </update> 23 </mapper>
测试所用的代码
MybatisAddTest:
1 package com.zyk.Test; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 import org.junit.Test; 11 12 import com.zyk.po.Customer; 13 14 public class MybatisAddTest { 15 @Test 16 public void test() throws IOException { 17 String resource="config.xml"; 18 InputStream inputStream=Resources.getResourceAsStream(resource); 19 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream); 20 SqlSession sqlSession=sessionFactory.openSession(); 21 Customer customer=new Customer(); 22 customer.setUsername("zhangyunkun"); 23 customer.setJobs("student"); 24 customer.setPhone("13734115929"); 25 int num=sqlSession.insert("com.zyk.mapper"+".CustomerMapper.addCustomer",customer); 26 if(num>0) { 27 System.out.println("success"+num); 28 }else { 29 System.out.println("fail"); 30 } 31 sqlSession.commit(); 32 sqlSession.close(); 33 } 34 }
MybatisDeleteTest:
1 package com.zyk.Test; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 import org.junit.Test; 11 12 public class MybatisDeleteTest { 13 @Test 14 public void test() throws IOException { 15 String resource="config.xml"; 16 InputStream inputStream=Resources.getResourceAsStream(resource); 17 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); 18 SqlSession sqlSession=sqlSessionFactory.openSession(); 19 int num=sqlSession.delete("com.zyk.mapper"+".CustomerMapper.deleteCustomer", 3); 20 if(num>0) { 21 System.out.println("success"); 22 }else { 23 System.out.println("fail"); 24 } 25 sqlSession.commit(); 26 sqlSession.close(); 27 } 28 29 30 }
MybatisUpdateTest:
package com.zyk.Test; import java.io.IOException; import java.io.InputStream; 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 com.zyk.po.Customer; public class MybatisUpdateTest { @Test public void tesrt() throws IOException { String resource="config.xml"; InputStream inputStream=Resources.getResourceAsStream(resource); SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession=sessionFactory.openSession(); Customer customer=new Customer(); customer.setId(3); customer.setUsername("zjh"); customer.setJobs("student"); customer.setPhone("15614141855"); int num=sqlSession.update("com.zyk.mapper"+".CustomerMapper.updateCustomer", customer); if (num>0) { System.out.println("success"); }else { System.out.println("fail"); } sqlSession.commit(); sqlSession.close(); } }
实验截图,由于直接执行完成只有最后的表:
实验问题:
前面执行代码输出success之后,数据库没有任何的变化,原因在于最后没有写sqlsession.commit()提交这一句导致有success,数据没有写到数据库中。
mybatis操作实验总结:
1.读取配置文件
String resource="config.xml"; InputStream inputStream=Resources.getResourceAsStream(resource);
2.根据配置文件创建SqlSessionFactory
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
3.通过SqlSessionFactory创建SqlSession
SqlSession sqlSession=sessionFactory.openSession();
4.使用SqlSession对象进行对数据库的操作
Customer customer=new Customer(); customer.setId(3); customer.setUsername("zjh"); customer.setJobs("student"); customer.setPhone("15614141855"); int num=sqlSession.update("com.zyk.mapper"+".CustomerMapper.updateCustomer", customer); if (num>0) { System.out.println("success"); }else { System.out.println("fail"); } sqlSession.commit();
5.关闭SqlSession
sqlSession.close();