本次测试的实例中的相关配置文件如下:
application.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="name" value="mysql"/>
<property name="url" value="xxxxxxx"/>
<property name="username" value="xxxxxxx"/>
<property name="password" value="zh4y4q5ang"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mybatis/*.xml"/>
<property name="typeAliasesPackage" value="com.entities"/>
<property name="configLocation" value="classpath:config/configuration.xml"/>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.interfaces"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
configuration.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>
<plugins>
<plugin interceptor="com.interceptors.LogInterceptor" ></plugin>
</plugins>
</configuration>
CityMapper.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.interfaces.CityMapper">
<resultMap id="BaseResultMap" type="com.entities.City">
<id column="ID" jdbcType="INTEGER" property="id"/>
<result column="Name" jdbcType="CHAR" property="name"/>
<result column="CountryCode" jdbcType="CHAR" property="countrycode"/>
<result column="District" jdbcType="CHAR" property="district"/>
<result column="Population" jdbcType="INTEGER" property="population"/>
</resultMap>
<select id="selectCityById" parameterType="int" resultType="com.entities.City">
SELECT * FROM city WHERE ID=#{id,jdbcType=INTEGER}
</select>
<insert id="insertCity" parameterType="com.entities.City">
insert into city
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
ID,
</if>
<if test="name != null">
Name,
</if>
<if test="countrycode != null">
CountryCode,
</if>
<if test="district != null">
District,
</if>
<if test="population != null">
Population,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=CHAR},
</if>
<if test="countrycode != null">
#{countrycode,jdbcType=CHAR},
</if>
<if test="district != null">
#{district,jdbcType=CHAR},
</if>
<