CustomerMapper.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.kaibing.mapper.CustomerMapper"> <!-- 根据ID查询 --> <select id="findCustomerById" parameterType="Integer" resultType="com.kaibing.pojo.Customer"> select * from t_customer where id = #{id} </select> <!-- 模糊查询 --> <select id="findCustomerByName" parameterType="String" resultType="com.kaibing.pojo.Customer"> select * from t_customer where username like '%${value}%' </select> <!-- 插入用户 --> <insert id="addCustomer" parameterType="com.kaibing.pojo.Customer"> insert into t_customer(username,jobs,phone) values(#{username},#{jobs},#{phone}) </insert> <!-- 更新 --> <update id="updateCustomer" parameterType="com.kaibing.pojo.Customer"> update t_customer set username=#{username},jobs=#{jobs},phone=#{phone} where id=#{id} </update> <!-- 删除 --> <delete id="deleteCustomer" parameterType="Integer"> delete from t_customer where id=#{id} </delete> </mapper>
MybitsTest.java
package com.kaibing.test; import java.io.IOException; import java.io.InputStream; import java.util.List; 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 com.kaibing.pojo.Customer; public class MybitsTest { public static void main(String[] args) { String resource = "mybits-config.xml"; // 读取配置文件 InputStream inputStream=null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 构建配置文件工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 创建会话 SqlSession sqlSession = sqlSessionFactory.openSession(); // 查询ById Customer customer = sqlSession.selectOne("com.kaibing.mapper.CustomerMapper" + ".findCustomerById",1); // 输出结果 System.out.println(customer.toString()); // 查询like ByName List<Customer> customers = sqlSession.selectList("com.kaibing.mapper.CustomerMapper" + ".findCustomerByName","g"); // 输出结果 System.out.println("|||||||||||||||||||||||"); for(Customer cust : customers) { System.out.println(cust); } // Customer insertCustomer = new Customer(); // insertCustomer.setJobs("大傻"); // insertCustomer.setPhone("11111111"); // insertCustomer.setUsername("wanger"); //// 插入 // Integer num = sqlSession.insert("com.kaibing.mapper.CustomerMapper" // + ".addCustomer",insertCustomer); //// 输出结果 // System.out.println("|||||||||||||||||||||||"); // System.out.println(num); Customer updateCustomer = new Customer(); updateCustomer.setId(4); updateCustomer.setJobs("二傻"); updateCustomer.setPhone("22222"); updateCustomer.setUsername("wangsan"); // 插入 Integer update = sqlSession.update("com.kaibing.mapper.CustomerMapper" + ".updateCustomer",updateCustomer); // 输出结果 System.out.println("|||||||||||||||||||||||--"); System.out.println(update); // 插入 Integer delete = sqlSession.delete("com.kaibing.mapper.CustomerMapper" + ".deleteCustomer",1); // 输出结果 System.out.println("|||||||||||||||||||||||"); System.out.println("delete"+delete); // 提交事务 sqlSession.commit(); // 关闭会话 sqlSession.close(); } }