Java 之Spring框架整合mybatis
一、配置maven
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <!-- 为Spring 核心提供了大量扩展 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <!-- C3P0连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> </dependencies>
二、applicationContext.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" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com"></context:component-scan> <!-- 加载jdbc.properties配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 使用c3p0数据源连接数据库 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 连接完数据库之后就拥有了访问数据库的能力,然后通过SqlSessionfactoryBean创建SqlSessionFactory工厂,并且绑定mybatis的核心配置文件和mapper.xml文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:com/dao/*.xml" /> </bean> <!-- 创建SqlSession对象 --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> </beans>
三、具体代码
AdminMapper.java
package com.mapper; import java.util.List; import com.entity.Admin; public interface AdminMapper { List<Admin> selectAdmin(); }
AdminMapperImpl.java
package com.mapper.impl; import java.util.List; import javax.annotation.Resource; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.stereotype.Component; import com.entity.Admin; import com.mapper.AdminMapper; @Component(value = "AdminMapper1") public class AdminMapperImpl implements AdminMapper{ @Resource(name = "sqlSession") private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<Admin> selectAdmin() { AdminMapper mapper = sqlSession.getMapper(AdminMapper.class); return mapper.selectAdmin(); } }
adminMapper.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.mapper.AdminMapper"> <select id="selectAdmin" resultType="com.entity.Admin"> select * from j_admin </select> </mapper>
运行代码
package com.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.entity.Admin; import com.mapper.AdminMapper; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); AdminMapper adminMapper = (AdminMapper) context.getBean("AdminMapper1"); List<Admin> admin = adminMapper.selectAdmin(); for(Admin i : admin){ System.out.println(i); } } }