发表于: 2017-05-03 14:28:38
2 1394
今天完成的事情:
①简单的了解一下mybatis配置文件中configuration下的10个节点。(properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers)
environments:
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--
如果上面没有指定数据库配置的properties文件,那么此处可以这样直接配置
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test1"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
-->
<!-- 上面指定了数据库配置文件, 配置文件里面也是对应的这四个属性 -->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<!-- 我再指定一个environment -->
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!-- 与上面的url不一样 -->
<property name="url" value="jdbc:mysql://localhost:3306/demo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
environments 节点可以配置多个 environment 子节点,例如两个 environment, 两个id分别对应开发环境(dev)和正式环境(final)。通过配置environments的default属性就能选择对应的environment了。
typeAliases:
<typeAliases>
<!--如果子节点是 typeAlias 节点,那么 mybatis 就会获取alias 和 type 两个属性的值 --><!-- <typeAlias type="com.hust.mybatis.po.User" alias="user"/> -->
<!-- 如果子节点是package,那么久获取package节点的name属性,mybatis会自动扫描包该路径的pojo,定义别名,别名默认为类名(首字母小写或大写)
-->
<package name="com.jn.bean"/></typeAliases>
typeHandlers:
例如 Student类的createAt和updateAt是Date类型,数据库中却是Long类型。就是我直接向数据库写数据,要写入的是一个Date对象,但是写到数据库之后这个Date对象就变成了Date对象所描述的时间到1970年的秒数了,然后当我从数据库读取这个秒数之后,系统又会自动帮我将这个秒数转为Date对象。
再比如我有一个User类,User类中有一个属性叫做interest,这个属性用来描述用户的爱好,它的数据类型是一个List集合,那么我想在把这个List集合存入数据库的时候能够自动的变成{XXX,XXX,XXX}
这样一个字符串然后存起来。
这个时候,那就需要自定义typeHandler,自定义typeHandler我们有两种方式,一种是实现TypeHandler接口,还有一种简化的写法就是继承自BaseTypeHandler类。
@MappedJdbcTypes({JdbcType.BIGINT})@MappedTypes({Date.class})
*在我们启用了我们自定义的这个TypeHandler之后,数据的读写都会被这个类所过滤
public class MyDateTypeHandler extends BaseTypeHandler<Date> {
*在setNonNullParameter方法中,我们重新定义要写往数据库的数据。
*在另外三个方法中我们将从数据库读出的数据类型进行转换。
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, String.valueOf(parameter.getTime()));
}
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
return new Date(rs.getLong(columnName));
}
public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return new Date(rs.getLong(columnIndex));
}
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getDate(columnIndex);
}
}
<insert id="insertStudent" parameterType="Student">
INSERT INTO
student (stuName, qq, courseTypeId, enrollTime, school, onlineID, will, create_at, update_at)values(#{stuName},#{qq},#{courseTypeId},#{enrollTime},#{school},#{onlineId},#{will},
#{createAt,typeHandler=com.jn.utils.MyDateTypeHandler},#{updateAt,typeHandler=com.jn.utils.MyDateTypeHandler})
</insert>
测试一下:
@Test
public void insertStudent(){
SqlSession sqlSession = sqlSessionFactory.openSession();
Student student = new Student();
student.setStuName("张三三");
student.setCreateAt(new Date());
student.setUpdateAt(new Date());
sqlSession.insert("com.jn.service.StudentService.insertStudent",student);
sqlSession.close();
}
OK,如此之后,就能实现将Date对象插入数据库之后变秒数以及将数据库中的秒数读取之后自动转为Date对象了。
查找时的BigInt数据转换为Date:
SqlSession sqlSession = sqlSessionFactory.openSession();
Student stu = sqlSession.selectOne("com.jn.service.StudentService.findStudentById", 142434260);
System.out.println(stu);
结果:
Student{id=142434260, stuName='张三五', qq='null', courseTypeId=null, enrollTime='null', school='null', onlineId='null', will='null', createAt=Wed May 03 22:16:53 CST 2017, updateAt=Wed May 03 22:16:53 CST 2017, onlineClassId='null', referer='null', auditor='null', fromWhere='null', dailyLink='null'}
②简单的了解了spring与mybatis的整合中需注意的地方。
③添加数据返回ID
④动态SQL:
明天计划的事情:
今天遇到的问题:
①向数据库插入There is no getter for property named 'onlineClassID' in 'class com.jn.bean.Student'
原因:
#{onlineClassId} 写成 #{onlineID}了
②当数据库字段与类字段不一致时,如果保证该字段取得值。
比如Student表有“create_at”,"update_at",这两个字段在Student类中是“createAt”,"updateAt"。
@Results(
value = {
@Result(column = "create_at", property = "createAt"),
@Result(column = "update_at", property = "updateAt")
})
②mybatis
收获:
评论