/**
* Mybatis 动态SQL/模糊查询/log4j
*
* 1、动态sql,可以在mapper中添加<if/> <when/>等标签,做逻辑判断做动态Sql
* 2、模糊查询
* 方法1:and userName like '%${userName}%' 用${...}
* 方法2:and userName like CONCAT(CONCAT('%', #{userName}), '%'); CONCAT(CONCAT('%', #{xxx}), '%')
* 3、log4j:添加log4j相关包,加入xml/properties配置文件即可
*
* @param args
*/
测试类:
package com.mybatis.test;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.mybatis.vo.User;
public class Test {
/**
* Mybatis 动态SQL/模糊查询/log4j
*
* 1、动态sql,可以在mapper中添加<if/> <when/>等标签,做逻辑判断做动态Sql
* 2、模糊查询
* 方法1:and userName like '%${userName}%' 用${...}
* 方法2:and userName like CONCAT(CONCAT('%', #{userName}), '%'); CONCAT(CONCAT('%', #{xxx}), '%')
* 3、log4j:添加log4j相关包,加入xml/properties配置文件即可
*
* @param args
*/
public static void main(String[] args) {
InputStream is = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
System.out.println(is);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
System.out.println(sessionFactory);
SqlSession session = sessionFactory.openSession();
System.out.println(session);
String userNameSpace = "com.mybatis.vo.User";
/*一对一嵌套结果*/
String getUserByCondition = userNameSpace+".getUserByCondition";
List<User> users = session.selectList(getUserByCondition,new User(1, "Tester2", 1));
System.out.println(users);
}
}
User类:
package com.mybatis.vo;
public class User {
private int id;
private String userName;
private int sex;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String userName, int sex) {
super();
this.id = id;
this.userName = userName;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", sex=" + sex + "]";
}
}
userMapper.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.mybatis.vo.User">
<select id="getUserByCondition" resultType="User" parameterType="User">
select * from User where 1=1
<if test="userName != null and userName != '' ">
and userName like '%${userName}%'
<!-- and userName like CONCAT(CONCAT('%', #{userName}), '%'); -->
</if>
<choose>
<when test="sex != 0">
and sex = #{sex}
</when>
<otherwise></otherwise>
</choose>
</select>
</mapper>
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${user.home}/foss-framework.log" />
<param name="append" value="true" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="100" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" />
</layout>
</appender>
<!-- 下面是打印mybatis语句的配置-->
<logger name="com.ibatis" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Connection" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Statement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.PreparedStatement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.ResultSet" additivity="true">
<level value="DEBUG" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.mybatis.vo.User" alias="User" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.1.92:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/vo/userMapper.xml" />
</mappers>
</configuration>