欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

mybatis基本增删改查、缓存,延迟加载以及别名的配置

发布时间:2025/3/20 编程问答 22 豆豆
生活随笔 收集整理的这篇文章主要介绍了 mybatis基本增删改查、缓存,延迟加载以及别名的配置 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. mybatis.xml/SqlMapConfig.xml 配置

jdbc.properties

#mysql connection jdbc_driver=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost:3306/mybatistest jdbc_user=root jdbc_password=123

mybatis.xml/SqlMapConfig.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"> <!-- 使用配置文件jdbc.properties,连接数据库的配置方式 --> <!--操作数据库配置信息,对数据进行映射--> <configuration><!-- 引入配置文件jdbc.properties --><properties resource="jdbc.properties"></properties><typeAliases><!-- 逐个设置别名方式如下: --><!-- <typeAlias alias="Student" type="com.java.dao.pojo.Student"/><typeAlias alias="Test" type="com.java.dao.pojo.Test"/> --><!-- 将指定包下的类都给定别名 --><package name="com.java.dao.pojo"/><!-- 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载 默认为false --><setting name="lazyLoadingEnabled" value="true" /><!-- 当开启时,任何方法的调用都会加载该对象的所有属性 默认false 备注:3.4.1版本前默认为true --><setting name="aggressiveLazyLoading" value="false" /><!-- 开启全局二级缓存 --><setting name="cacheEnabled" value="true"/></typeAliases><!-- 连接数据库 --><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${jdbc_driver}" /><property name="url" value="${jdbc_url}" /><property name="username" value="${jdbc_user}" /><property name="password" value="${jdbc_password}" /></dataSource></environment></environments> <!--相关mappers配置文件--> <mappers><mapper resource="com/java/dao/pojo/StudentMapper.xml"/> </mappers> </configuration>

2.基本增删改的mapper.xml配置

StudentMapper.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.java.dao.idao.IPlayerDao"> <!-- 开启PlayerMapper的一级缓存 --><cache></cache><!-- 根据id查询和查询列表 --><select id="findById" parameterType="String" resultType="com.java.dao.pojo.Student">select * from student where id=#{id}</select><select id="findAll" resultType="com.java.dao.pojo.Student">select id,name,age from student;</select><!-- 增删改 --><insert id="insert">insert into student(id,name,age) values(#{id},#{name},#{age});</insert><delete id="delete" parameterType="String">delete from student where id=#{id}</delete><update id="update" parameterType="com.java.dao.pojo.Student">update student set name=#{name},age=#{age} where id=#{id};</update></mapper>

总结

以上是生活随笔为你收集整理的mybatis基本增删改查、缓存,延迟加载以及别名的配置的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。