Mybatis笔记整理3(mybatis整合spring)

通过ApplicationContext来加载applicationConfig.xml文件,这个文件中主要是加载数据库链接文件he配置连接池信息,注入Mybatis的工厂和mapper的

pojo类在前文中写过,这里省略

sqlMapConfig.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>
	
	<!-- 别名 包以其子包下所有类   头字母大小都行-->
	<typeAliases>
<!-- 		<typeAlias type="com.itheima.mybatis.pojo.User" alias="User"/> -->
		<package name="com.itheima.mybatis.pojo"/>
	</typeAliases>
	
	<mappers>
		<package name="com.itheima.mybatis.mapper"/>
	</mappers>
	

	
</configuration>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

   <context:property-placeholder  location="classpath:db.properties"/>
   
   <!-- 数据库连接池 -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<!-- Mybatis的工厂 -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- 核心配置文件的位置 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
	</bean>
	
	<!-- 原始Dao开发 -->
	<bean id="userDao" class="com.itheima.mybatis.dao.UserDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
	</bean>
	<!-- Mapper动态代理开发 -->
	<!-- <bean id="userMapper"  class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
		<property name="mapperInterface"  value="com.itheima.mybatis.mapper.UserMapper" />
	</bean> -->
	
	<!-- Mapper动态代理开发,扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.itheima.mybatis.mapper" />
	</bean>
</beans>

mapper接口

public interface UserMapper {
    
    
	public User selectUserById(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.itheima.mybatis.mapper.UserMapper">
	<!-- 通过ID查询一个用户 -->
	<select id="selectUserById" parameterType="Integer" resultType="User">
		select * from user where id = #{
    
    v}
	</select>
</mapper>

测试查询操作

public class JunitTest {
    
    
	@Test
	public void testMapper() throws Exception {
    
    
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationConfig.xml");
		UserMapper mapper = ac.getBean(UserMapper.class);
		//UserMapper mapper = (UserMapper) ac.getBean("userMapper");
		User user = mapper.selectUserById(10);
		System.out.println(user);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42260782/article/details/108848752