一、简单使用
1、创建Maven项目
2、导入相关jar在pom.xml
3、在数据库创建表
4、建立持久化类pojo,跟表的列对应
5、mapper.xml映射文件,在这里写sql,“自定义”传值在下面表述
<?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="ink.poesy.mapper.UserMapper">
<select id="userDate" resultType="ink.poesy.pojo.User">
select * from user
</select>
</mapper>
6、配置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="computer_mysql">
<environment id="computer_mysql">
<!-- jdbc事务管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源 -->
<!-- 用mabatis自带POOLED -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!-- 修改为你自己的数据库 -->
<property name="url" value="jdbc:mysql://localhost:3306/poesy?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 配置映射文件的路径 -->
<mappers>
<!-- 改为你自己的配置地址 -->
<mapper resource="ink/poesy/mapper/UserMapper.xml"/>
</mappers>
</configuration>
7、log4j.properties:日志、键值对
8、测试方法:
(1)读取核心文件
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
(2)根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
(3)通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
(4)通过SqlSession操作数据库,增删改查
List<User> list = sqlSession.selectList("ink.poesy.mapper.UserMapper.userDate");
(5)关闭SqlSession
sqlSession.close();
二、传值使用
其余部分不变,改变映射文件,详见代码注释
<?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="ink.poesy.mapper.UserMapper">
<!-- resultType表示类型,parameterType表示传入类型 -->
<select id="userDate" resultType="ink.poesy.pojo.User" parameterType="ink.poesy.pojo.User">
select
*
from
user
while
<!-- 为了保证不传值依然执行成功 -->
1 = 1
<!-- 此处加一个判断如果传进值不为空或不为null,则执行条件 -->
<!-- #{}在Mybatis中是占位符,相当于原来的?传值中的? -->
<if test="user_id != null and user_id != ''">
and `user_id` = #{user_id}
</if>
<!-- 用户名,一般用模糊查询,在mybatis中模糊查询用函数concat来拼 -->
<if test="user_name != null and user_name != ''">
and `user_name` like concat('%',#{user_name},'%');
</if>
<!-- 密码 -->
<if test="user_pwd != null and user_pwd != ''">
and `user_pwd` = #{user_pwd}
</if>
<!-- 邮箱 -->
<if test="user_email != null and user_email != ''">
and `user_email` = #{user_email}
</if>
<!-- 电话 -->
<if test="user_tel != null and user_tel != ''">
and `user_tel` = #{user_tel}
</if>
<!-- 性别 -->
<if test="user_sex != null and user_sex != ''">
and `user_sex` = #{user_sex}
</if>
<!-- 年龄 -->
<if test="user_age != null and user_age != ''">
and `user_age` = #{user_age}
</if>
<!-- 工作或学校 -->
<if test="user_work != null and user_work != ''">
and `user_work` = #{user_work}
</if>
<!-- 简介 -->
<if test="user_show != null and user_show != ''">
and `user_show` = #{user_show}
</if>
</select>
</mapper>
调用处修改为
User userparameter = new User();
userparameter.setUser_name("wenlei");
List<User> list = sqlSession.selectList("ink.poesy.mapper.UserMapper.userDate", userparameter);