SSM15.4【Mybatis:Mybatis的多表操作总结】

 

 


 

复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 3 
 4 <!--The content of element type "configuration" must match
 5 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,
 6 objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)".-->
 7 
 8 <configuration>
 9 
10     <!--加载外部properties文件-->
11     <properties resource="jdbc.properties"></properties>
12 
13     <!--自定义别名-->
14     <typeAliases>
15         <typeAlias type="com.haifei.domain.User" alias="user" />
16         <typeAlias type="com.haifei.domain.Order" alias="order"/>
17         <typeAlias type="com.haifei.domain.Role" alias="role"/>
18     </typeAliases>
19 
20     <!--注册类型处理器-->
21     <typeHandlers>
22         <typeHandler handler="com.haifei.handler.DateTypeHandler"/>
23     </typeHandlers>
24 
25     <environments default="development">
26         <!--配置数据源环境-->
27         <environment id="development">
28             <transactionManager type="JDBC"></transactionManager>
29             <dataSource type="POOLED">
30                 <property name="driver" value="${jdbc.driver}"/>
31                 <property name="url" value="${jdbc.url}"/>
32                 <property name="username" value="${jdbc.username}"/>
33                 <property name="password" value="${jdbc.password}"/>
34             </dataSource>
35         </environment>
36     </environments>
37 
38     <!--加载映射文件-->
39     <mappers>
40         <mapper resource="com/haifei/mapper/UserMapper.xml"/>
41         <mapper resource="com/haifei/mapper/OrderMapper.xml"/>
42     </mappers>
43 
44 </configuration>
复制代码
复制代码
 1 package com.haifei.handler;
 2 
 3 import org.apache.ibatis.type.BaseTypeHandler;
 4 import org.apache.ibatis.type.JdbcType;
 5 
 6 import java.sql.CallableStatement;
 7 import java.sql.PreparedStatement;
 8 import java.sql.ResultSet;
 9 import java.sql.SQLException;
10 import java.util.Date;
11 
12 public class DateTypeHandler extends BaseTypeHandler<Date> {
13 
14     /**
15      * 将java数据类型 转换为 数据库所需的数据类型
16      * @param preparedStatement
17      * @param i
18      * @param date
19      * @param jdbcType
20      * @throws SQLException
21      */
22     @Override
23     public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
24         long time = date.getTime();
25         preparedStatement.setLong(i, time);
26     }
27 
28     /**
29      * 将数据库中的数据类型 转换为 所需的java数据类型
30      * @param resultSet  查询出的结果集
31      * @param s  要转换的字段名称
32      * @return
33      * @throws SQLException
34      */
35     @Override
36     public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
37         //获取结果集中需要的数据类型(long)转换为Date类型,并返回
38         long aLong = resultSet.getLong(s);
39         Date date = new Date(aLong);
40         return date;
41     }
42 
43     /**
44      * 将数据库中的数据类型 转换为 所需的java数据类型
45      * @param resultSet
46      * @param i
47      * @return
48      * @throws SQLException
49      */
50     @Override
51     public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
52         long aLong = resultSet.getLong(i);
53         Date date = new Date(aLong);
54         return date;
55     }
56 
57     /**
58      * 将数据库中的数据类型 转换为 所需的java数据类型
59      * @param callableStatement
60      * @param i
61      * @return
62      * @throws SQLException
63      */
64     @Override
65     public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
66         long aLong = callableStatement.getLong(i);
67         Date date = new Date(aLong);
68         return date;
69     }
70 }
复制代码

 

posted @   yub4by  阅读(48)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示