飞行的猪哼哼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

重新创建一个CustomerMapper的接口:

package com.student.mapper;

import com.student.po.Customer;

public interface CustomerMapper {
	public Customer findCustomerById(Integer id);

}

Mapper文件名字必须和接口相同。

<?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.student.mapper.CustomerMapper">
	<select id="findCustomerById" resultType="customer" parameterType="Integer">
		select * from t_customer where id = #{id}
	</select>
</mapper>


applicationContext中增加这段代码:


	<bean id ="customerMapper" 
	class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.student.mapper.CustomerMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>

增加后的结果:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!--读取db.properties文件 -->
	<context:property-placeholder
		location="classpath:db.properties" />

	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="maxIdle" value="${jdbc.maxIdle}"></property>
		<property name="initialSize" value="${jdbc.initialSize}"></property>
	</bean>

	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!--开启事务注解 -->
	<tx:annotation-driven
		transaction-manager="transactionManager" />

	<!-- sqpring 与mybatis整合 -->
	<!-- 配置mybatis工厂 -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation"
			value="classpath:mybatis-config.xml"></property>
	</bean>

	<!--实例化DAO -->
	<bean id="customerDao"
		class="com.student.dao.impl.CustomerDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	
	<bean id ="customerMapper" 
	class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.student.mapper.CustomerMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
</beans>

mybatis-config配置文件修改当前的CustomerMapper的位置。

<?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>
	<typeAliases>
		<package name="com.student.po"/>
	</typeAliases>
	<mappers>
		<mapper resource="com/student/mapper/CustomerMapper.xml" />
	</mappers>
</configuration>

测试文件:

package com.student.text;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.student.dao.CustomerDao;
import com.student.mapper.CustomerMapper;
import com.student.po.Customer;

public class DaoTest {
/*
	@Test
	public void findCustomerByIdDao() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDao");
		Customer customer = customerDao.findCustomerById(1);
		System.out.println(customer);
		
	}
	*/
	@Test
	public void findCustomerByIdMapperTest() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerMapper customerMapper = applicationContext.getBean(CustomerMapper.class);
		Customer customer = customerMapper.findCustomerById(1);
		System.out.println(customer);
		
	}

}

测试结果:

Sat Oct 19 19:50:40 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Sat Oct 19 19:50:40 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Sat Oct 19 19:50:40 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Sat Oct 19 19:50:40 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Sat Oct 19 19:50:40 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Sat Oct 19 19:50:40 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
DEBUG [main] - ==>  Preparing: select * from t_customer where id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 1
Customer [id=1, username=joy, jobs=doctor, phone=123344555]

注意:
1.Mapper接口的名称必须和Mapper.xml映射文件的名称相同。
2.Mapper.xml文件的namespace与Mapper接口的类路径相同(接口文件和映射文件在同一个包中)
3.Mapper接口中的方法名和Mapper.xml中定义的每个执行语句的id相同。
4.Mapper接口中方法的输入参数类型和Mapper.xml中定义的sql的parameterType类型相同。

posted on 2019-10-19 20:03  飞行的猪哼哼  阅读(57)  评论(0)    收藏  举报