MyBatis核心配置文件
jdbc. driver= com. mysql. cj. jdbc. Driver
jdbc. url= jdbc: mysql: / / 192.168 .29 .201 : 3306 / mybatis
jdbc. username= root
jdbc. password= 123456
< configuration>
< properties resources = " jdbc.properties/>
<!-- 设置类型别名-->
<typeAliases>
<!--
type:设置需要设置别名的类型,
alias:设置某个类型的别名,若此项不设置,则类型别名默认为类名,且不区分大小写
-->
<!--<typeAlias type=" com.lotus.mybatis.pojo.User" alias = " User" > </ typeAlias> -->
< package name = " com.lotus.mybatis.pojo" />
</ typeAliases>
< environments default = " development" >
< environment id = " development" >
< ! --
type:JDBC | MANAGED
JDBC: 表示当前环境中,执行SQL时,使用的是JDBC中原生的事务管理方式,事务提交和回滚需要手动处理
MANAGED:表示被管理,例如Spring
-- >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " mappers/UserMapper.xml" />
</ mappers>
</ configuration>
< mappers>
< package name = " com.lotus.mybatis.mapper" />
</ mappers>
设置配置文件模板
①复制要创建模板的配置文件内容 ② ③
MyBatis获取参数值
Mybatis获取参数值的两种方式:${} 和 #{} ${}本质就是字符串拼接,#{}本质是占位符赋值 ${}使用字符串拼接的方式拼接SQL,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但#{}使用占位符赋值的方式拼接SQL,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
private String sex;
private String email;
public User ( ) {
}
public User ( Integer id, String username, String password, Integer age, String sex, String email) {
this . id = id;
this . username = username;
this . password = password;
this . age = age;
this . sex = sex;
this . email = email;
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getUsername ( ) {
return username;
}
public void setUsername ( String username) {
this . username = username;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
public Integer getAge ( ) {
return age;
}
public void setAge ( Integer age) {
this . age = age;
}
public String getSex ( ) {
return sex;
}
public void setSex ( String sex) {
this . sex = sex;
}
public String getEmail ( ) {
return email;
}
public void setEmail ( String email) {
this . email = email;
}
@Override
public String toString ( ) {
return "com.lotus.mybatis.pojo.User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
'}' ;
}
}
public class SqlSessionUtils {
public static SqlSession getSqlSession ( ) {
SqlSession sqlSession = null ;
try {
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder ( ) ;
InputStream is = Resources . getResourceAsStream ( "mybatis-config.xml" ) ;
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder. build ( is) ;
sqlSession = sqlSessionFactory. openSession ( true ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
return sqlSession;
}
}
单个字面量类型的参数
若mapper接口中的方法参数为单个字面量类型,可以使用KaTeX parse error: Expected 'EOF', got '#' at position 4: {}或#̲{}以任意名称获取参数的值,*… {}手动加单引号**
public interface ParameterMapper {
User getUserByUsername ( String username) ;
}
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.lotus.mybatis.mapper.ParameterMapper" >
< select id = " getUserByUsername" resultType = " com.lotus.mybatis.pojo.User" >
select * from t_user where username='${username}'
</ select>
</ mapper>
@Test
public void testGetUserByUsername ( ) throws IOException {
SqlSession sqlSession = SqlSessionUtils . getSqlSession ( ) ;
ParameterMapper mapper = sqlSession. getMapper ( ParameterMapper . class ) ;
User user = mapper. getUserByUsername ( "dizzy" ) ;
System . out. println ( user) ;
}
多个字面量类型的参数
若mapper接口中的方法参数为多个时,MyBatis自动将这些参数放在一个Map集合中,以arg0,arg1…为键,以参数为值,以param1,param2…为键,以参数为值,只需要通过KaTeX parse error: Expected 'EOF', got '#' at position 4: {}或#̲{}访问map集合的键就可以获… {}需手动加单引号**
public interface ParameterMapper {
User checkLogin ( String username, String password) ;
}
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.lotus.mybatis.mapper.ParameterMapper" >
< select id = " checkLogin" resultType = " User" >
select * from t_user where username='${arg0}' and password='${param2}'
</ select>
</ mapper>
@Test
public void testCheckLogin ( ) throws IOException {
SqlSession sqlSession = SqlSessionUtils . getSqlSession ( ) ;
ParameterMapper mapper = sqlSession. getMapper ( ParameterMapper . class ) ;
User user = mapper. checkLogin ( "dizzy" , "12345" ) ;
System . out. println ( user) ;
}
map集合类型的参数
若mapper接口中的方法参数为多个时,可手动创建map集合,将这些数据放在map中,只需通过KaTeX parse error: Expected 'EOF', got '#' at position 4: {}或#̲{}访问map集合的键就可以获… {}需手动加单引号**
User checkLoginByMap ( Map < String , Object > map) ;
< select id = " checkLoginByMap" resultType = " User" >
select * from t_user where username='${username}' and password='${password}'
</ select>
@Test
public void testCheckLoginByMap ( ) throws IOException {
SqlSession sqlSession = SqlSessionUtils . getSqlSession ( ) ;
ParameterMapper mapper = sqlSession. getMapper ( ParameterMapper . class ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map. put ( "arg0" , "dizzy" ) ;
map. put ( "arg1" , "12345" ) ;
map. put ( "param1" , "dizzy" ) ;
map. put ( "param2" , "12345" ) ;
map. put ( "username" , "dizzy" ) ;
map. put ( "password" , "12345" ) ;
User user = mapper. checkLoginByMap ( map) ;
System . out. println ( user) ;
}
实体类类型的参数(重点)
若mapper接口中的方法参数为实体类时,只需通过KaTeX parse error: Expected 'EOF', got '#' at position 4: {}或#̲{}访问实体类属性值的方式就可… {}需手动加单引号**
int insertUser ( User user) ;
< insert id = " insertUser" >
insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
</ insert>
@Test
public void testInsertUser ( ) throws IOException {
SqlSession sqlSession = SqlSessionUtils . getSqlSession ( ) ;
ParameterMapper mapper = sqlSession. getMapper ( ParameterMapper . class ) ;
int res = mapper. insertUser ( new User ( null , "water" , "123456" , 11 , "男" , "water@qq.com" ) ) ;
System . out. println ( res) ;
}
使用注解@param命名参数(重点)
若mapper接口中的方法参数使用@param修饰,MyBatis自动将这些参数放在一个Map集合中,以@param注解的值为键,以参数为值,以param1,param2…为键,以参数为值,只需要通过KaTeX parse error: Expected 'EOF', got '#' at position 4: {}或#̲{}访问map集合的键就可以获… {}需手动加单引号**
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>
@Test
public void testCheckLoginByParam ( ) throws IOException {
SqlSession sqlSession = SqlSessionUtils . getSqlSession ( ) ;
ParameterMapper mapper = sqlSession. getMapper ( ParameterMapper . class ) ;
User user = mapper. checkLoginByParam ( "water" , "123456" ) ;
System . out. println ( user) ;
}