package com.kang.mapper; import java.util.List; import com.kang.pojo.User; public interface UserMapper { //根據用戶id查詢用戶信息 public User findUserById(int id) throws Exception; //查詢用戶列表 public List<User> findUserByUsername(String username) throws Exception; //添加用戶信息 public void insertUser(User user)throws Exception; }
<?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.kang.mapper.UserMapper"> <!-- 注意這裏的 namespace必須對應着map接口的全類名--> <select id="findUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select> <select id="findUserByUsername" parameterType="java.lang.String" resultType="user"> select * from user where username like '%${value}%' </select> <insert id="insertUser" parameterType="user"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select LAST_INSERT_ID() </selectKey> insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) </insert> </mapper>
<!-- 加載 映射文件 --> <mappers> <mapper resource="map/UserMapper.xml" /> </mappers>
package com.kang.test; import java.io.IOException; import java.io.InputStream; import java.util.Date; 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 org.junit.Before; import org.junit.Test; import com.kang.mapper.UserMapper; import com.kang.pojo.User; public class UserTest { // 會話工廠 private SqlSessionFactory sqlSessionFactory; @Before public void createSqlSessionFactory() throws IOException { // 配置文件SqlMapConfig.xml在類路徑下 String resource = "SqlMapConfig.xml"; // 獲得配置文件流 InputStream inputStream = Resources.getResourceAsStream(resource); // 建立會話工廠,傳入mybatis的配置文件信息 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testFindUserById() throws Exception { //獲取session SqlSession session = sqlSessionFactory.openSession(); //獲取mapper接口的代理對象 UserMapper userMapper = session.getMapper(UserMapper.class); //調用代理對象方法 User user = userMapper.findUserById(27); System.out.println(user); //關閉session session.close(); System.out.println("---------執行完畢-----------"); } @Test public void testFindByUsername() throws Exception{ //獲取session SqlSession session = sqlSessionFactory.openSession(); //獲取mapper接口的代理對象 UserMapper userMapper = session.getMapper(UserMapper.class); //調用代理對象方法 List<User> list=userMapper.findUserByUsername("張"); for (User user : list) { System.out.println(user); } //關閉session session.close(); System.out.println("---------執行完畢-----------"); } @Test public void testInsert() throws Exception{ User user=new User(); user.setUsername("cxk"); user.setSex("1"); user.setBirthday(new Date()); user.setAddress("四川成都"); SqlSession session=sqlSessionFactory.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); userMapper.insertUser(user); session.commit(); //關閉session session.close(); System.out.println("---------執行完畢-----------"); } }
五、單元測試html
id查詢測試java
模糊查詢測試sql
插入數據測試apache