黑马学习MyBatis 用MyBatis对表进行条件查询 模糊查询 动态sql
1 package cn.itcast.domain; 2 3 /* 4 CREATE TABLE `message` ( 5 `id` int(11) NOT NULL, 6 `command` varchar(16) DEFAULT NULL, 7 `description` varchar(30) DEFAULT NULL, 8 `content` varchar(2048) DEFAULT NULL, 9 PRIMARY KEY (`id`) 10 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 11 */ 12 public class Message { 13 private int id; 14 private String command; 15 private String description; 16 private String content; 17 18 public Message(){} 19 20 public int getId() { 21 return id; 22 } 23 24 public void setId(int id) { 25 this.id = id; 26 } 27 28 public String getCommand() { 29 return command; 30 } 31 32 public void setCommand(String command) { 33 this.command = command; 34 } 35 36 public String getDescription() { 37 return description; 38 } 39 40 public void setDescription(String description) { 41 this.description = description; 42 } 43 44 public String getContent() { 45 return content; 46 } 47 48 public void setContent(String content) { 49 this.content = content; 50 } 51 52 @Override 53 public String toString() { 54 return "Message{" + 55 "id=" + id + 56 ", command='" + command + '\'' + 57 ", description='" + description + '\'' + 58 ", content='" + content + '\'' + 59 '}'; 60 } 61 }
1 package cn.itcast.dao; 2 3 import cn.itcast.domain.Message; 4 5 import java.util.List; 6 7 public interface MessageDao { 8 9 Message findById(int id); 10 11 //mybatis只支持单个参数,如果有多个参数将它们封装为对象 12 // List<Message> queryMessageList(String command, String description); 13 14 List<Message> queryMessageList(Message condition); 15 }
1 package cn.itcast.mapper; 2 3 import cn.itcast.domain.Message; 4 import org.apache.ibatis.io.Resources; 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import java.io.IOException; 12 import java.io.Reader; 13 import java.util.List; 14 15 public class MessageMapperTest { 16 SqlSessionFactory sqlSessionFactory; 17 18 @Before 19 public void init() throws IOException { 20 //加载配置 21 Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); 22 //构建工厂 23 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 24 } 25 26 @Test 27 public void testFindById() throws IOException { 28 SqlSession sqlSession = sqlSessionFactory.openSession(true); 29 Message message = sqlSession.selectOne("cn.itcast.mapper.MessageMapper.findById", 2); 30 System.out.println(message); 31 } 32 33 34 @Test 35 public void testQueryMessageList(){ 36 //准备条件 37 /*Message message = new Message(); 38 message.setCommand("百"); 39 message.setDescription("百");*/ 40 41 String command = "百"; 42 String description = "百"; 43 Message message = new Message(); 44 message.setCommand("%" + command + "%"); 45 message.setDescription("%" + description + "%"); 46 47 SqlSession sqlSession = sqlSessionFactory.openSession(true); 48 List<Message> messageList = sqlSession.selectList("cn.itcast.mapper.MessageMapper.queryMessageList", message); 49 System.out.println(messageList); 50 } 51 52 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <typeAliases> 8 <typeAlias type="cn.itcast.domain.Message" alias="Message"></typeAlias> 9 </typeAliases> 10 11 <environments default="development"> 12 <environment id="development"> 13 <transactionManager type="JDBC"></transactionManager> 14 <dataSource type="POOLED"> 15 <property name="driver" value="com.mysql.jdbc.Driver"></property> 16 <property name="url" value="jdbc:mysql://localhost:3307/micro_message"></property> 17 <property name="username" value="root"></property> 18 <property name="password" value="root"></property> 19 </dataSource> 20 </environment> 21 </environments> 22 23 <mappers> 24 <mapper resource="cn/itcast/mapper/MessageMapper.xml"></mapper> 25 </mappers> 26 27 </configuration>
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3307/micro_message jdbc.username=root jdbc.password=root
# priority :debug<info<warn<error #you cannot specify every priority with different file for log4j log4j.rootLogger=debug,xx #console log4j.appender.xx=org.apache.log4j.ConsoleAppender log4j.appender.xx.layout=org.apache.log4j.PatternLayout log4j.appender.xx.layout.ConversionPattern= [%d{yyyy-MM-dd HH:mm:ss a}]:%p %l%m%n
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="cn.itcast.mapper.MessageMapper"> 6 7 <select id="findById" resultType="Message"> 8 select * from message where id=#{id} 9 </select> 10 11 <!--<select id="queryMessageList" parameterType="Message" resultType="Message"> 12 select * from message 13 <where> 14 <if test="command != null"> 15 and command like "%"#{command}"%" 16 </if> 17 <if test="description != null"> 18 and description like "%"#{description}"%" 19 </if> 20 </where> 21 </select>--> 22 23 <!--<select id="queryMessageList" parameterType="Message" resultType="Message"> 24 select * from message 25 <where> 26 <if test="command != null"> 27 and command like #{command} 28 </if> 29 <if test="description != null"> 30 and description like #{description} 31 </if> 32 </where> 33 </select>--> 34 35 <!--<select id="queryMessageList" parameterType="Message" resultType="Message"> 36 select * from message 37 <where> 38 <if test="command != null"> 39 and command like '%' #{command} '%' 40 </if> 41 <if test="description != null"> 42 and description like '%' #{description} '%' 43 </if> 44 </where> 45 </select>--> 46 47 <select id="queryMessageList" parameterType="Message" resultType="Message"> 48 select * from message 49 <where> 50 <if test="command != null && !"".equals(command.trim())"> 51 and command like '%' #{command} '%' 52 </if> 53 <!--<if test="command != null and !"".equals(command.trim())"> 54 and command like '%' #{command} '%' 55 </if>--> 56 <if test="description != null"> 57 and description like '%' #{description} '%' 58 </if> 59 </where> 60 </select> 61 62 </mapper>