1.主配置文件
基本格式
1.导入config约束(必不可少的部分)
<?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(配置)
properties(属性):绑定外部properties文件中的数据库,也可使用<property/>标签自定义,但是识别的话外部配置文件的优先级更高
settings(设置):是MyBatis重要的调整设置,可改变运行时行为,实现懒加载、日志、缓存开关等,详细用法查看文档MyBatis帮助文档
typeAliases(类型别名):只能用于XML文件的配置,旨在降低冗余的全限定类名书写,在XML文件中使用到该类的全限定类名的地方都可以使用该别名替换
也可以使用注解,在该类上加上@Alias("别名") 注意导包import org.apache.ibatis.type.Alias;
<!-- 绑定指定的数据库-->
<properties resource="db.properties">
<property name="username" value="root"/>
</properties>
<settings>
<!-- 标准日志实现-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- LOG4J日志-->
<!-- 1.导入依赖,Maven官网找-->
<!-- 2.要写LOG4J的配置文件.properties,网上找不用特殊记忆-->
<!-- 3.会生成日志文件-->
<!-- <setting name="logImpl" value="LOG4J"/>-->
</settings>
<typeAliases>
<typeAlias type="全限定类名" alias="别名"></typeAlias>
</typeAliases>
- environments(环境配置):
可以配多种环境,但是SqlSessionFactory只能识别一种,通过default以及id属性进行匹配
environment(环境变量)
transactionManager(事务管理器):两种类型 type="[JDBC|MANAGED]"
dataSource(数据源):使用标准的 JDBC 数据源接口来配置 JDBC 连接对象的资源。
UNPOOLED– 这个数据源的实现会每次请求时打开和关闭连接,慢。
<常用>POOLED– 这种数据源的实现利用“池”的概念将 JDBC 连接对象组织起来,避免了创建新的连接实例时所必需的初始化和认证时间。
JNDI – 这个数据源实现是为了能在如 EJB 或应用服务器这类容器中使用,容器可以集中或在外部配置数据源,然后放置一个 JNDI 上下文的数据源引用。
<!-- 环境配置-->
<environments default="mysql">
<!-- 环境变量-->
<environment id="mysql">
<!-- 配置事务管理器-->
<transactionManager type="JDBC"/>
<!-- 数据源-->
<dataSource type="POOLED">
<!-- 配置数据库连接-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="*******"/>
</dataSource>
</environment>
</environments>
4.mappers(映射器):
绑定映射配置文件,告诉MyBatis去哪里找定义的SQL语句
1.<mapper resource="映射配置文件路径使用 / 做目录分隔符"></mapper>
2.<mapper class="全限定接口名,要求接口的名称和映射配置文件名相同"/>
3.<package name="全限定包名,要求接口的名称和映射配置文件名相同"/>
<mappers>
<mapper resource="dao/InfoMapper.xml"></mapper>
<!-- <mapper class="com.itcode.dao.UserMapper"/>-->
<!-- <package name="com.itcode.dao"/>-->
</mappers>
2.映射配置文件
基本格式
1.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">
2.
1.<mapper namespace="全限定接口名"> id属性的值是该接口的某个方法名 —— 通过这两个的值准确定位到某个方法
2.resultType是某个类的全限定类名,指定SQL语句返回的类型
<!--namespace必须是某个接口的全限定接口名-->
<mapper namespace="com.itcode.dao.UserMapper">
<!-- id是该接口的某个方法名-->
<!-- resultType是某个类的全限定类名-->
<select id="findAll" resultType="com.itcode.pojo.User">
select * from user;
</select>
<select id="selectById" parameterType="int" resultType="com.itcode.pojo.User">
select * from user where id=#{id};
</select>
<insert id="addUser" parameterType="com.itcode.pojo.User" >
insert into user(id,username,birthday,sex,address) values (#{id},#{username},#{birthday},#{sex},#{address});
</insert>
<update id="updateUser" parameterType="com.itcode.pojo.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id};
</update>
<delete id="deleteUser">
delete from db.user where id=#{id};
</delete>
</mapper>
#{} 和 ${} 有什么区别?
#{}相当于PreparedStatement对象,预编译SQL,可以防止SQL注入,更安全
${}相当与Statement对象,SQL拼接,有SQL注入的风险
3.CRUD操作
1.select查找 —— 在
<mapper>标签里定义

2.增、删、改
增删改操作一定要提交事务
(1)手动提交:sqlSession.commit();
(2)打开自动提交:sqlSessionFactory.openSession(true);

拓展:当POJO的属性列表和数据可字段不匹配的解决方案


(1):起别名,映射配置文件中操作
<select id="" resultType="">
select id,name,pwd as password from info;
</select>
(2):使用结果集映射 —— ResultMap
在接口的方法上直接使用注解,讲SQL直接写入注解中即可,简化操作不用写映射配置文件,对于简单的SQL可用,稍复杂的SQL有局限性
推荐使用XML映射配置文件
public interface InfoMapper {
//增
@Insert("insert into info(id,username,pwd) values(#{id},#{username},#{pwd})")
int addInfo(Info info);
//查
@Select("select * from info")
List<Info> findAll();
}


被折叠的 条评论
为什么被折叠?



