**要求:可以通过编号查询,也可以通过产品标题进行模糊查询。**
项目结构
ProductMapper.java
package com.chinasofti.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.chinassofti.beans.product;
public interface ProductMapper {
/*
* @Select("<script>" +"select * from product" +"<where>"
* +" <if test=\"title != null and title != ''\">"
* +" title like CONCAT('%',#{title},'%')" +"</if>" +"</where>" +"</script>")
*/
@Select("<script>"
+ "select * from product"
+ "<where>"
+ " <if test=\"title != null and title != ''\">"
+ " title like CONCAT('%', #{title}, '%')"
+ " </if>"
+ "</where>"
+ "</script>")
//ResultType Results 可以不要默认自动绑定
@ResultType(product.class)
@Results({
@Result(column="id", property="id"),
@Result(column="title", property="title"),
@Result(column="price", property="price")
})
List<product> selectPoduct(product pro);
}
实体类
package com.chinassofti.beans;
public class product {
private Integer id;
private String title;
private String sell_point;
private Integer price;
private Integer num;
@Override
public String toString() {
return "product [id=" + id + ", title=" + title + ", sell_point=" + sell_point + ", price=" + price + ", num="
+ num + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSell_point() {
return sell_point;
}
public void setSell_point(String sell_point) {
this.sell_point = sell_point;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
}
测试类
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.chinasofti.mapper.ProductMapper;
import com.chinassofti.beans.product;
public class test {
public static void main(String[] args) throws IOException {
//读取配置文件
InputStream InputStream = Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactory 对象
SqlSessionFactory sessionfactory = new SqlSessionFactoryBuilder().build(InputStream);
SqlSession SqlSession = sessionfactory.openSession();
ProductMapper session = SqlSession.getMapper(ProductMapper.class);
product pro=new product();
//设置模糊查询的值
pro.setTitle("");
List<product> rs = session.selectPoduct(pro);
for (product product : rs) {
System.out.println(product);
}
/*
* SqlSession session = MyBatisUtils.getsession(); UserMapper UserMapper =
* session.getMapper(UserMapper.class);
*/
}
}
配置文件 jdbc.properties
jdbcUrl=jdbc:mysql://localhost:3306/group6?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
mybatis核心配置文件
<?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.chinassofti.beans.User" alias="UserAlias"/>
</typeAliases>
-->
//引入配置文件
<properties resource="jdbc.properties"></properties>
<!-- 配置MyBatis的环境 -->
<environments default="env">
<!-- 配置一个环境 -->
<environment id="env">
<!-- 配置事务管理器 JDBC(有事务)、MANAGED(托管) -->
<transactionManager type="JDBC" />
<!-- 配置数据源 JNDI(web服务器方式定义数据源)、POOLED(自带连接池)、UNPOOLED(不带连接池) -->
<dataSource type="POOLED">
<!-- 连接数据库驱动 -->
<property name="driver" value="${driverClassName}" />
<!-- 连接数据库URL -->
<property name="url" value="${jdbcUrl}" />
<!-- 连接数据库用户名 -->
<property name="username" value="${username}" />
<!-- 连接数据库密码 -->
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 配置XxxMapper.xml访问数据库SQL文件 -->
<mappers>
<mapper class="com.chinasofti.mapper.ProductMapper"/>
<!-- <mapper resource="com/chinassofti/beans/UserMapper.xml"/> -->
</mappers>
</configuration>