Mybatis配置

生成配置文件

mybatis.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">
  <!--default 默认连接 -->
   <environments default="development">
    <environment id="development">
    <!-- 使用原生JDBC事务-->
      <transactionManager type="JDBC"/>
      <!-- 使用连接池-->
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>

添加dtd文件

http://mybatis.org/dtd/mybatis-3-config.dtd

配置映射文件Mapper

<?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">
  <!-- namespace 实现类的全路径-->
<mapper namespace="org.mybatis.example.BlogMapper">
<!-- id为方法名,
		parameterType 参数类型 
		resultType 返回值类型
		如果返回值为List resultTye 写List 的泛型
-- >
  <select id="selectBlog" parameterType="int" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>
		InputStream is = Resources.getResourceAsStream("");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		
		SqlSession session = factory.openSession();
		List<Object> selectList = session.selectList("");
		session.close();

环境配置详解

<transactionManager type=""/>
  1. jdbc 原生事务
  2. MANAGED 把事务管理交给其他容器
<dataSource type="POOLED">
  1. UNPOOLED 不适用数据库连接池
  2. POOLED 使用数据库连接池 提高系统运行效率
  3. JNDI java命名接口技术

tomcat配置数据库连接

在web项目中META-INF中添加context.xml文件
context.xml文件中添加配置信息

<?xml version="1.0" encoding="UTF-8"?>
<context>
	<Resource
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://localhost:3306/ssm"
		username="root"
		password="root@123"
		maxAtive="50"
		maxIdle="20"
		name="test"
		auth="Container"
		maxWait="10000"
		type="javax.sql.DataSource"
	/>
</context>

	InitialContext context = new InitialContext();
	DataSource ds = (DataSource) context.lookup(""

settings 标签配置

Mybatis 接口配置

  1. mybatis.xml
  2. mapper.xml
  3. Interface
  4. mapper = session.getMapper();
  5. mapper.id();

在接口中实现多参数

  1. 在接口中声明方法
  2. 用注解@Param

动态SQl

<if> 标签
<where> 标签 去掉第一个AND
<choose> <when> <otherwise> 其中一个成立,其他不执行
< set > 在修改SQL中set动态拼接 去掉最后一个逗号
<trim> 增加 一个单词 去掉一个
< bind> 重新赋值
<foreach> 适用于in 批量新增

< include > refid属性

缓存

<mapper namespace="">
	<!--开启缓存  不写readOnly 需要将bean 序列化-->
	<cache readOnly="true"></cache>
	<select id=""  ></select>
</mapper>

SqlSessionFactory缓存

二级缓存
当数据频繁被查询,很少被修改 时候使用
有效范围=是同一个factory 内SQLSession都可以访问
sqlSession对象close 或commit时候 会将数据flush 到缓存中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值