模糊查询与动态sql
来源:互联网 发布:红警点网络进不去 编辑:程序博客网 时间:2024/04/27 01:16
1.导入Mybatis和Mysql相关jar包
2.建立数据库Mybatis并且建表User表,表里字段 id,name,age
3.mybatis配置文件 conf.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> <!-- 对事务的管理和连接池的配置 --> <!-- development :开发模式 --> <!-- work:工作模式 --> <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://localhost:3306/Mybatis" /> <property name="username" value="root" /> <property name="password" value="" /> </dataSource> </environment> </environments> <!-- mapping 文件路径配置 --> <mappers> <!-- <mapper resource="com/zw/mybatis/studentsMapper.xml" /> --> <!-- <mapper class="com.zw.mybatis.StudentsMapper" /> --> <mapper resource="com/zw/mybatis/Mapper/UserMapper.xml" /> </mappers> </configuration>
4.实体类User
package com.zw.mybatis.model;public class User { private int id; private String name; private int age; private int bumenid; private Bumen bumen; //关联表查询的关键 private Teacher teacher;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getBumenid() {return bumenid;}public void setBumenid(int bumenid) {this.bumenid = bumenid;}public User(int id, String name, int age, int bumenid) {super();this.id = id;this.name = name;this.age = age;this.bumenid = bumenid;}public User() {super();}public Bumen getBumen() {return bumen;}public void setBumen(Bumen bumen) {this.bumen = bumen;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;} }
5.查询条件实体类
package com.zw.mybatis.Condition;public class UserCondition {private String name;private int MinAge;private int MaxAge;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getMinAge() {return MinAge;}public void setMinAge(int minAge) {MinAge = minAge;}public int getMaxAge() {return MaxAge;}public void setMaxAge(int maxAge) {MaxAge = maxAge;}public UserCondition(String name, int minAge, int maxAge) {super();this.name = name;MinAge = minAge;MaxAge = maxAge;}public UserCondition() {super();}}
6.UserMapper与UserMapper.xml
UserMapper
package com.zw.mybatis.model;public interface UserMapper {}
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.zw.mybatis.model.UserMapper"><!-- <select id="selectnameandteachnamebyid" parameterType="int" resultMap="newselectMap">select b.Teachername,a.name from User a,Teacher b where a.id=b.userid and a.id=#{0}</select><resultMap type="com.zw.mybatis.model.User" id="newselectMap"><result property="name" column="name" /><collection property="teacher" ofType="com.zw.mybatis.model.Teacher"><result property="Teachername" column="Teachername" /></collection></resultMap> --><select id="getuser" parameterType="com.zw.mybatis.Condition.UserCondition" resultType="com.zw.mybatis.model.User">select * from user where <if test='name != "%null%"'>name like #{name} and </if>age between #{MinAge} and #{MaxAge}</select></mapper>
7.测试类
package com.zw.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.zw.mybatis.Condition.UserCondition;import com.zw.mybatis.model.User;import com.zw.mybatis.model.UserMapper;public class UserTest {public static void main(String[] args) {String resourse = "conf.xml";InputStream is = UserTest.class.getClassLoader().getResourceAsStream(resourse);SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);SqlSession session = factory.openSession(true);UserCondition parameter=new UserCondition("%zhang%", 20, 25);List<User> list=session.selectList("com.zw.mybatis.model.UserMapper.getuser", parameter);System.out.println("姓名:"+list.get(0).getName());}}
0 0
- 动态SQL与模糊查询
- 模糊查询与动态sql
- 动态SQL与模糊查询
- MyBatis-动态sql与模糊查询 -07
- mybatis 动态SQL与模糊查询
- Mybatis动态SQL与模糊查询
- mybatis的动态SQL与模糊查询
- Mybatis学习总结之动态SQL与模糊查询
- Mybatis学习笔记-动态SQL与模糊查询
- Mybatis学习总结之动态SQL与模糊查询
- 【Mybatis学习总结六】动态SQL与模糊查询
- MyBatis动态查询 模糊查询实例sql
- Delphi与SQL模糊查询
- Delphi与SQL模糊查询
- Mybatis 1.动态sql 2.模糊查询
- 03. mybatis 动态sql && 模糊查询
- Mybatis学习----模糊查询和动态sql
- 六、Mybatis动态SQL和模糊查询
- GDB(三):gdb 常用命令
- 递归巩固
- win7+opencv-3.1.0+vs2015配置实用方法
- GDB(四):gdb process
- 王老师讲C语言结构
- 模糊查询与动态sql
- GDB(五):gdb 脚本
- OV7670摄像采集图像在VGA上显示的分析和实现,精确到每个时钟,每行代码。
- 关于模拟登陆的小结-抓包、cookie、session和token
- 自定义activity切换动画
- JAVA编程基础(三) 面向对象的编程思想与原则
- 关于Struct2 代码没错,加载网页莫名出现404问题。
- MFC学习经验(转)
- java 乱码详解_jsp中pageEncoding、charset=UTF -8"、request.setCharacterEncoding("UTF-8")