Mybatis的使用

一、简单使用

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&amp;characterEncoding=utf-8&amp;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);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

园长的牧歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值