MyBatis获取参数值的两种方式:
1.字符串拼接:${}
2.占位符赋值:#{}
一些准备工作
java/cc/mllt/sky/utils
resources/cc/mllt/sky/utils
封装SQLSessionUtils工具类
package cc.mllt.sky.utils; 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 java.io.IOException; import java.io.InputStream; public class SqlSessionUtils { public static SqlSession getSqlSession(){ SqlSession sqlSession = null; try{ InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); sqlSession = sqlSessionFactory.openSession(true); }catch (IOException e){ e.printStackTrace(); } return sqlSession; } }
MyBatis获取参数值的各种情况
1.mapper接口方法的参数为单个的字面量类型
或者这样
总结
可以通过
${}
和#{}
以任意的字符串获取参数值,但是需要注意${}
的单引号问题
2.mapper接口方法的参数为多个时
总结
mapper接口方法的参数为多个时,MyBatis会将这些参数放在map集合中,以两种方式进行存储,
第一种为:arg0,arg1……为键,以参数为值
第二种为:以param1,param2……为键,以参数为值
因此只需要通过
#{}
和${}
以键的形式访问值就可以了。
3.mapper接口方法的参数有多个时,可以手动将这些参数放在一个map中存储
4.mapper接口方法的参数是一个实体类类型的参数
实体类:属性 属性值
Map:键 值
通过#{}或${}以属性的方式访问值即可
必须是属性名字,不能乱写噢!
5.使用@Param命名参数
此时Mybatis会将这些参数放在一个Map集合中,以两种方式存储
a. 以@Param注解的值为键,以参数为值
b. 以param1,param2……为键,以参数为值
通过#{}或${}以键的方式访问值即可
源码贴贴
目录
cc.mllt.sky.utils.UserParameterMapper.java
package cc.mllt.sky.utils; import cc.mllt.sky.jdbc.User; import org.apache.ibatis.annotations.Param; import java.util.Map; public interface UserParameterMapper { /** * 登录验证 * @param username String 用户名 * @param password String 密码 * @return User对象 */ User UserLogin(String username,String password); /** * 验证登陆 * @return User对象 */ User UserLoginByMap(Map<String,Object> map); /** * 验证登陆(使用@Param注解) * @param username 用户名 * @param password 用户密码 * @return User对象 */ User UserLoginByParam(@Param("name") String username, @Param("password") String password); /** * 根据用户账号查询用户信息 * @return User对象 */ User getUserByUserCount(String usercount); /** * 添加用户 * @param user 用户对象 * @return int型 */ int UserAdd(User user); }
cc.mllt.sky.jdbc.User.java
package cc.mllt.sky.jdbc; import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Timestamp; public class User { private BigInteger userId; public String userName; private String userCount; private String userPassword; private int userGrade; public Timestamp userRegDate; private BigDecimal userBlance; public User( String userName,String userCount,String userPassword) { this.userName=userName; this.userCount=userCount; this.userPassword=userPassword; } //getter and setter public BigInteger getUserId() { return userId; } public void setUserId(BigInteger userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserCount() { return userCount; } public void setUserCount(String userCount) { this.userCount = userCount; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public int getUserGrade() { return userGrade; } public void setUserGrade(int userGrade) { this.userGrade = userGrade; } public Timestamp getUserRegDate() { return userRegDate; } public void setUserRegDate(Timestamp userRegDate) { this.userRegDate = userRegDate; } public BigDecimal getUserBlance() { return userBlance; } public void setUserBlance(BigDecimal userBlance) { this.userBlance = userBlance; } @Override public String toString(){ return "User{"+ "userId='"+userId+"'"+ ",userName='"+userName+"'"+ "userCount='"+userCount+"'"+ ",userPassword='"+userPassword+"'"+ "userGrade='"+userGrade+"'"+ ",userRegDate='"+userRegDate+"'"+ "userBlance='"+userBlance+"'}"; } }
resources.cc.mllt.sky.utils.UserParameterMapper.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="cc.mllt.sky.utils.UserParameterMapper"> <!-- User getUserByUsedCount(String usercount);--> <select id="getUserByUserCount" resultType="User"> <!--select * from t_users where `userCount` = #{username}--> select * from t_users where `userCount` = '${username}' </select> <!-- User UserLogin(String username,String password);--> <select id="UserLogin" resultType="User"> <!--select * from t_users where userCount=#{arg0} and userPassword=#{arg1}--> <!--select * from t_users where userCount=#{param1} and userPassword=#{param2}--> <!--select * from t_users where userCount=#{param1} and userPassword=#{arg1}}--> select * from t_users where userCount='${param1}' and userPassword='${arg1}' </select> <!-- User UserLoginByMap(Map<String,Object> map);--> <select id="UserLoginByMap" resultType="User"> select * from t_users where userCount=#{username} and userPassword=#{password} </select> <!-- int UserAdd(User user);--> <insert id="UserAdd"> insert into t_users set `userName`=#{userName}, `userCount`=#{userCount}, `UserPassword`=#{userPassword}, `userGrade`='0', `userBlance`='0'; </insert> <!-- User UserLoginByParam(@Param("name") String username, @Param("password") String password);--> <select id="UserLoginByParam" resultType="User"> select * from t_users where userCount=#{name} and userPassword=#{password} </select> </mapper>
Test
下面有一些测试方法是以前用到的,在本次笔记中没有用到的,可以删掉
import cc.mllt.sky.jdbc.User; import cc.mllt.sky.jdbc.UsersMapper; import cc.mllt.sky.utils.SqlSessionUtils; import cc.mllt.sky.utils.UserParameterMapper; 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.jupiter.api.Test; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.util.HashMap; import java.util.List; import java.util.Map; public class TestUsers { @Test public void UsersAdd() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); mapper.insertUser(); } @Test public void UsersDel() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); mapper.deleteUser(); } @Test public void UsersUpdate() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); mapper.updateUser(); } @Test public void UsersGetById() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); User user = mapper.selectUserById(); System.out.println(user); System.out.println(user.getUserName()); } @Test public void UsersGetAll() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); List<User> list = mapper.selectUser(); list.forEach(user -> System.out.println(user)); } @Test public void testJDBC() throws Exception{ String username ="xrilang"; Class.forName(""); //创建链接对象 Connection connection = DriverManager.getConnection(""); //PreparedStatement ps = connection.prepareStateme nt("selsect * from t_users where userName = '"+username+"'");//麻烦且不安全,会造成SQL注入 PreparedStatement ps = connection.prepareStatement("select * from t_users where userName= ? "); ps.setString(1,username); } @Test public void GetUserByCount(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); UserParameterMapper mapper = sqlSession.getMapper(UserParameterMapper.class); User user = mapper.getUserByUserCount("test001"); System.out.println(user); } @Test public void CheckUserLogin(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); UserParameterMapper mapper = sqlSession.getMapper(UserParameterMapper.class); User user = mapper.UserLogin("test001","123456改了密码"); System.out.println(user); } @Test public void CheckUserLoginByMap(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); UserParameterMapper mapper = sqlSession.getMapper(UserParameterMapper.class); Map<String,Object> map = new HashMap<>(); map.put("username","test001"); map.put("password","123456改了密码"); User user = mapper.UserLoginByMap(map); System.out.println(user); } @Test public void UserAdd(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); UserParameterMapper mapper = sqlSession.getMapper(UserParameterMapper.class); int result = mapper.UserAdd(new User("张三","mllt@xrilang.com","123")); System.out.println(result); } @Test public void CheckUserLoginByParam(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); UserParameterMapper mapper = sqlSession.getMapper(UserParameterMapper.class); Map<String,Object> map = new HashMap<>(); User user = mapper.UserLoginByParam("test001","123456改了密码"); System.out.println(user); } }