一、MyBatis配置文件详解
<?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 配置文件
settings mybatis设置
typeAliases 为Java类起别名
typeHandlers 类处理器
objectFactory 对象工厂
plugins 插件
environments 环境
transactionManager : 事务管理
dataSource : 数据源
mappers 映射器
二、优化代码
优化配置文件
在resources目录下建立一个database.properties,在这里我所遇到的一个小问题,就是在优化配置文件之前,我们是直接在mybatis-config.xml中直接写的关于数据库的配置文件,以及设置编码格式,在xml中,里面的&必须和amp连在一块,但是我们设置成配置文件的时候,就不需要那个amp,分号也不需要。代码如下:
driver=com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
接下来在mybatis核心配置文件中(mybatis-config.xml)引入properties配置文件,并用${}表达式引入其中的值。
<?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 resource="database.properties"/>
<environments default="first">
<environment id="first">
<transactionManager type="JDBC"></transactionManager>
<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/westos/dao/UserMapper.xml"/>
</mappers>
</configuration>
优化别名
<!--配置别名-->
<typeAliases>
<!--配置指定类的别名-->
<typeAlias type="org.westos.pojo.User" alias="User"/>
<!--也可以为一个包的所有类指定别名,这个别名为类名
org.westos.pojo.User - > User
org.westos.pojo.Student - > Student
-->
<package name="org.westos.pojo"/>
</typeAliases>
优化完毕就可以在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">
<!--mapper标签的namespace对应Mapper接口的类,包名+类名-->
<mapper namespace="com.kuang.dao.UserDao">
<!--select标签的id对应映射接口的方法名字 resultType:返回结果的类型 中间就编写sql语句-->
<select id="selectUser" resultType="User">
select * from user
</select>
<select id="selectUserById" resultType="User">
select * from user where id = #{id}
</select>
<!--
我们需要接受一个自定义的对象(引用对象),需要设置parameterType,为参数类型
接受这个对象的值,直接使用 #{对象字段名}
-->
<insert id="addUser" parameterType="User">
insert into user(id ,name, pwd) values (#{id},#{name},#{pwd})
</insert>
<delete id="deleteUserByID" parameterType="int">
delete from user where id = #{id}
</delete>
<update id="updateUser" parameterType="User">
update user set name =#{name},pwd = #{pwd} where id = #{id}
</update>
</mapper>
三、映射器
- resource(推荐)
- url
- class
- package
四、解决属性名与字段名不一致
我们将数据库和是实体类的相关字段和属性设置成下面,接下来我们进行相关测试
数据库:字段名 id name pwd
实体类:属性名 id name password
现在我们进行查询,看会发生什么情况。
我们可以发现代码没有错,但是查出的密码是空值了,那么这到底是为什么了?
结果分析
我们的查询语句是 select * from mybatis.user;等价于 select id,name,pwd from mybatis.user;
mybatis会根据数据库的字段名去找对应的实体类的属性名,(它会将所有列名转换为小写,然后去找实体类中对应的set方法,set方法后面的字段就对应数据库的字段名,如果不一样就会返回空值)
解决方法
1.修改set方法的名字,这里我们将set的名字改为数据库中对应的字段名。【不建议使用】
2.给SQL语句取别名【字段少的时候我们可以使用,但是多了就不建议使用】
3.结果集映射ResultMap【推荐方式】
这里需要注意的是,我们给包或者类起别名实在MyBatis核心配置文件中设置的,结果集映射是在对应的mapper.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对应的Mapper接口的类,包名+类名-->
<mapper namespace="org.westos.dao.UserMapper">
<resultMap id="UserMap" type="User">
<!--
一般通过id标签来映射主键
column = 数据库的列明
property = 结果集对应的数据库黎明的映射名
-->
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<!--select标签的id对应映射接口的方法名字,resultType对应的是返回结果的类型-->
<select id="selectUser" resultMap="UserMap" >
select * from mybatis.user
总的来说,这里需要注意在哪里设置,不然就会出错,另外也知道了查询出现空值的根本原因以及相应的三种解决方法,各有有缺。