mybatis的应用能大大简化我们编写程序的代码并提升效率。
package com.aurora.mybatis.test; import com.aurora.mybatis.mapper.ParameterMapper; import com.aurora.mybatis.pojo.User; import com.aurora.mybatis.utils.SqlSessionUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; public class ParameterMapperTest { @Test public void testGetAllUser(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); List<User> list = mapper.getAllUser(); list.forEach(user -> System.out.println(user)); } @Test public void testGetUserByUsername(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); User admin = mapper.getUserByUsername("admin"); System.out.println(admin); } @Test public void testCheckLogin(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); User user = mapper.checkLogin("admin","123456"); System.out.println(user); } @Test public void testCheckLoginByMap(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); Map<String , Object> map = new HashMap<>(); map.put("username","admin"); map.put("password","123456"); User user = mapper.checkLoginByMap(map); System.out.println(user); } @Test public void testInsertUser(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); int result = mapper.insertUser(new User(null, "李四", "123", 23, "男", "123@qq.com")); System.out.println(result); } @Test public void testCheckLoginByParam(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); User user = mapper.checkLoginByParam("admin","123456"); System.out.println(user); } }
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.aurora.mybatis.mapper.ParameterMapper"> <!--List<User> getAllUser();--> <select id="getAllUser" resultType="User"> select * from t_user </select> <!-- User getUserByUsername(String username);--> <select id="getUserByUsername" resultType="User"> select * from t_user where username = #{username} </select> <!-- User checkLogin(String username,String password);--> <!-- arg0 arg1 param1 param2--> <select id="checkLogin" resultType="User"> select * from t_user where username= #{arg0} and password = #{arg1} </select> <!-- User checkLoginByMap(Map<String, Objects> map);--> <select id="checkLoginByMap" resultType="User"> select * from t_user where username= #{username} and password = #{password} </select> <!-- int insertUser(User user);--> <insert id="insertUser"> insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email}) </insert> <!--User checkLoginByParam(@Param("username") String username, @Param("password") String password);--> <select id="checkLoginByParam" resultType="User"> select * from t_user where username= #{username} and password = #{password} </select> </mapper>标签:username,mapper,9.20,ParameterMapper,sqlSession,user,import From: https://www.cnblogs.com/kongxiangzeng/p/18632550