搭建Mybatis环境

1、导包

将依赖包拷贝到指定目录
导包

2、在src目录下面新建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>
	<!-- 配置连接相关的参数 
		default:在多个环境中默认使用哪个环境
	-->
	<environments default="dev">
		<!-- 生产环境 -->
		<!-- <environment id="prod"></environment> -->
		
		<!-- 测试环境 -->
		<environment id="dev">
			<!-- 事务管理 type:指定事务管理的方式 -->
			<transactionManager type="JDBC"/>
			
			<!-- 数据源 连接相关 type:是不是用连接池形式 POOLED(使用连接池) UNPOOL(不使用连接池) -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/demo?useUnicode=true&amp;characterEncoding=utf-8"/>
				<property name="username" value="root"/>
				<property name="password" value="123456"/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- 注册mapper配置文件 -->
	<mappers>
		<mapper resource="com/baizhi/dao/EmpDAOMapper.xml"/>
	</mappers>
</configuration>

3、在需要的地方创建mybatis的映射文件

在这里插入图片描述

<?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.baizhi.dao.EmpDAO">

	<!-- 查询:
		select标签 id:接口中的方法名 
		parameterType:参数类型 resultType:
		处理结果集,自动封装 当查询的字段名与实体属性名不一致时封装对象时拿不到值,可以数据库别名的方式写 -->
	<select id="findById" parameterType="Integer"
		resultType="com.baizhi.entity.Emp">
		select uid as id,name nickName from userlogin_userinfo
		where uid=#{id}
	</select>

	<!-- 当查询的字段名与实体属性名不一致时封装对象时拿不到值,可以数据库别名的方式写 -->
	<select id="findByIdAndName" resultType="com.baizhi.entity.Emp">
		select uid id,name as
		nickName from userlogin_userinfo where uid=#{uid} and name=#{name}
	</select>


	<!-- 无参数的情况 -->
	<select id="findAll" resultType="com.baizhi.entity.Emp">
		select uid id,name as nickName
		from userlogin_userinfo
	</select>
	
	<!-- 更新数据 -->
	<update id="updateData" parameterType="com.baizhi.entity.Emp">
		update userlogin_userinfo set name=#{nickName} where uid=#{id}
	</update>
	
	<!-- 删除数据 -->
	<delete id="deleteData" parameterType="Integer">
		delete from userlogin_userinfo where uid=#{id}
	</delete>
	
	<!-- 插入数据 -->
	<insert id="insertData" parameterType="com.baizhi.entity.Emp">
		insert into userlogin_userinfo() values(#{id},#{username},#{password},#{nickName})
	</insert>
</mapper>

4、如何使用?

public class TestMybatis{
	public static void main(String[] args){
		// 读取配置
		InputStream is = Resources.getResourceAsStream("mybatis-config.xml");

		// 创建sqlsessionfactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		// 获取连接 connection
		SqlSession session = sqlSessionFactory.openSession();

		// 直接通过sqlsession获取DAO对象(接口.class)
		EmpDAO empDAO = session.getMapper(EmpDAO.class);

		// 调用方法
		Emp emp = empDAO.findById(1);
		System.out.println(emp);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值