mybatis 配置文件
解析配置文件
- 配置mybatis-config.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">
<configuration>
// 引入properties文件,resource表示读取本项目中的属性文件,url读取远程属性文件,二者不能同时存在,否则报异常,从下文中的源码中可以看到
<!--<properties resource="classpath:app.properties"></properties>-->
<!--<properties url="http://localhost:8000/mybatis/app.properties"></properties>-->
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列别名替换列名 默认:true -->
<setting name="useColumnLabel" value="true" />
<!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<!-- 自定义typeHandle,将TIMESTAMP转为long -->
<typeHandlers>
<typeHandler handler="com.ws.mybatis.dao.LongTimeHandler" javaType="long" jdbcType="TIMESTAMP"/>
</typeHandlers>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="sidNkyy6h0wkNinH"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserInfoMapper.xml"/>
<!-- <mapper class="com/ws/mybatis/dao/UserInfoMapper"/>-->
</mappers>
</configuration>
app.properties
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123456
UserInfoMapper.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.ws.mybatis.dao.UserInfoMapper">
<resultMap id="BaseResultMap" type="com.ws.mybatis.model.UserInfo">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name_str" jdbcType="VARCHAR" property="nameStr" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="salary" jdbcType="DECIMAL" property="salary" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
<!-- 添加类型映射 -->
<result column="update_long_time" jdbcType="TIMESTAMP" property="updateLongTime" />
</resultMap>
<sql id="BaseTable">
user_info
</sql>
<select id="get" resultMap="BaseResultMap" parameterType="java.lang.Object" >
select id,name_str,age
from user_info
where id = #{id,jdbcType=INTEGER}
</select>
<select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Object" >
select u.*,u.update_time as update_long_time
from user_info u
where id = #{id,jdbcType=INTEGER}
</select>
<insert id="insert" parameterType="com.ws.mybatis.model.UserInfo" useGeneratedKeys="true" keyProperty="id">
insert into user_info(id,name_str,age)
values (#{id, jdbcType=INTEGER},#{nameStr, jdbcType=VARCHAR},#{age, jdbcType=INTEGER})
</insert>
</mapper>