(练习demo)mybatis 注解方法 模糊查询

**要求:可以通过编号查询,也可以通过产品标题进行模糊查询。**

项目结构在这里插入图片描述
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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值