SSM15.4【Mybatis:Mybatis的多表操作總結】
阿新 • • 發佈:2021-07-25
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 }