MyBatis中的数据库动态查询的编写

1.po中建立Student等持续化类。


2.配置mybatis-config.xml和log4j.properties文件。

3.编写实现类,接口StudentMapper中写操作的方法,配置文件中进行配置

public interface StudentMapper {
List<Student> find(Student stu);
List<Student> find2(Student stu);
List<Student> find3(Student stu);
List<Student> find4(List<Integer> ids);
List<Student> find5(Integer ids[]);
void update(Student stu);
}

4.StudentMapper.xml配置

<mapper namespace="com.dao.StudentMapper">
将sql语句可复用部分进行封装
<sql id="column_name">
id,name,age,gender,address
</sql>
<sql id="query_where">
<if test="name!=null">
and name =#{name}
</if>
<if test="age!=null">
and age=#{age}
</if>
</sql>

查询的方法

        <select id="find"  parameterType="student"  resultType="student" >
select 
<include refid="column_name"/>
from student where  1=1
<include refid="query_where"/>
</select>

<select id="find2" parameterType="student" resultType="student" >
select id,name,age,gender,address from student 

<!-- 
where 的作用:1如果第一条件不为null,则加上 where  2第一个条件的and或者or会被忽略 
-->
<where>
<include refid="query_where"/>
<if test="gender!=null">
and gender=#{gender}
</if>
</where>
</select>

<select id="find3" parameterType="student" resultType="student" >
select id,name,age,gender,address from student

<!--
如果where条件不起作用,则使用trim元素,trim不能与where同时使用 
前面是where 如果后面直接跟 and或者or的 则会忽略掉 
-->
<trim prefix="where" prefixOverrides="and|or">
<if test="address!=null">
and address =#{address}
</if>
<if test="age!=null">
and age=#{age}
</if>
</trim> 
</select>

查询结果放入集合中
<select id="find4" resultType="student" > <!-- select * from student where id in(3,5,6) -->
select * from student 
<where>
<foreach collection="list" item="item" separator="," open="id in (" close=")" >
#{item} 
</foreach>
</where>  
</select>
查询结果放入数组中
<select id="find5" resultType="student" > <!-- SELECT * FROM student WHERE id=3 OR id=5 OR id=6-->
select * from student 
<where>
<foreach collection="array" item="item" separator="or"  >
id=#{item} 
</foreach>
</where>  
</select>
修改数据库
<update id="update" parameterType="student">
update student
<!-- set 的作用:会自动处理好最后一列(加不加","号) 更改列名值后建议都加上","-->
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="age!=null">
age=#{age},
</if>
</set>
where id=#{id}
</update>
</mapper>

5.编写测试类进行测试(举例编写)

private SqlSessionFactory sf;//工厂只要创建一回
@Before
public void before(){
InputStream in = null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
sf = new SqlSessionFactoryBuilder().build(in);
}

普通动态查询

@Test
public void testFind() {
SqlSession session = sf.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setName("少林");
stu.setAge(89);
mapper.find(stu );
session.close();
}

@Test
public void testFind4() {
SqlSession session = sf.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Integer> list = Arrays.asList(3,5,6);
mapper.find4(list);
session.close();
}

@Test
public void testFind5() {
SqlSession session = sf.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Integer[] array = {3,5,6};
mapper.find5(array );
session.close();
}

@Test
public void testUpdate(){
SqlSession session = sf.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setName("少林者");
stu.setAge(99);
stu.setId(2);
mapper.update(stu);
session.commit();
session.close();
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一种Java持久化框架,它可以将SQL语句映射到Java方法,方便我们查询数据库。下面是使用MyBatis实现数据库查询的步骤: 1. 添加MyBatis依赖 在pom.xml文件添加以下依赖: ``` <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> ``` 2. 配置MyBatis 在resources目录下创建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> <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/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/mapper/MyMapper.xml"/> </mappers> </configuration> ``` 其,driver、url、username和password是数据库连接信息,需要根据实际情况修改。 3. 创建Mapper 在com.example.mapper包下创建MyMapper.java接口,并添加以下内容: ``` public interface MyMapper { List<User> getAllUsers(); } ``` 其User是实体类,数据库的用户。 4. 创建Mapper XML文件 在com.example.mapper包下创建MyMapper.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.example.mapper.MyMapper"> <select id="getAllUsers" resultType="com.example.entity.User"> select * from user </select> </mapper> ``` 其,resultType指定查询结果的类型。 5. 编写代码 在代码使用MyBatis查询数据库,示例如下: ``` SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")); try (SqlSession sqlSession = sessionFactory.openSession()) { MyMapper myMapper = sqlSession.getMapper(MyMapper.class); List<User> users = myMapper.getAllUsers(); for (User user : users) { System.out.println(user); } } ``` 其,SqlSessionFactory是MyBatis的核心类,用于创建SqlSession对象。SqlSession是与数据库交互的核心类,通过getMapper方法获取Mapper对象,并调用其的方法查询数据库。 以上就是使用MyBatis实现数据库查询的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值