作者:暧qs祢生 | 来源:互联网 | 2023-08-16 17:16
使用配置
配置文件中使用配置处理resultType以及使用外部properties
处理resultType
当查询语句中的resultMap对应实体类时,如果实体类与数据库结构不一致,那么返回的值就是null,需要处理数据库表结构字段与实体类之间的字段映射。
<select id="findAll" resultMap="myUser">
select * from user
select>
增加实体类与数据库表结构映射
<resultMap id="myUser" type="com.example.domain.User">
<id column="id" property="userId">id>
<result column="name" property="userName">result>
<result column="sex" property="userSex">result>
<result column="address" property="userAddress">result>
<result column="birthDay" property="userBirthDay">result>
resultMap>
然后在select insert等节点上的resultMap引用这个id即可。
使用propertites配置数据库信息
增加jdbcConfig.propertites
配置文件
jdbc.driver:com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql://localhost:3306/scmdb?serverTimezOne=UTC&useUnicode=true&characterEncoding=UTF-8
jdbc.username:root
jdbc.password:123456
然后在mybatis中使用这个配置文件
<properties resource="jdbcConfig.properties">
properties>
<environments default="myBatis">
<environment id="myBatis">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
配置文件总览
SqlMapConfigure.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbcConfig.properties">
properties>
<environments default="myBatis">
<environment id="myBatis">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="com/example/dao/IUserDao.xml">mapper>
mappers>
configuration>
jdbcConfig.properties
jdbc.driver:com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql://localhost:3306/scmdb?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
jdbc.username:root
jdbc.password:123456
IUserDao.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.example.dao.IUserDao">
<!--配置查询所有-->
<resultMap id="myUser" type="com.example.domain.User">
<id column="id" property="userId"></id>
<result column="name" property="userName"></result>
<result column="sex" property="userSex"></result>
<result column="address" property="userAddress"></result>
<result column="birthDay" property="userBirthDay"></result>
</resultMap>
<select id="findAll" resultMap="myUser">
select * from user
</select>
<select id="findUserById" resultType="com.example.domain.User">
select * from user where id=#{value}
</select>
<insert id="saveUser" parameterType="com.example.domain.User">
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
select last_insert_id();
</selectKey>
insert into user(userName,address,sex,birthday) values (#{userName},#{address},#{sex},#{birthDay});
</insert>
<update id="updateUser" parameterType="com.example.domain.User">
update user set userName=#{userName},address=#{address},sex=#{sex},birthday=#{birthDay} where id=#{id};
</update>
<delete id="deleteUser">
delete from user where id=#{value};
</delete>
<select id="findUserByQueryVo" parameterType="com.example.domain.QueryVo" resultType="com.example.domain.User">
select * from user where username like #{user.userName}
</select>
</mapper>