Spring框架 — Spring与Mybatis整合
1.在pom.xml中导入相关坐标依赖
<!-- spring版本号 -->
<properties>
<spring.version>4.3.6.RELEASE</spring.version>
</properties>
<dependencies>
<!--Spring相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--引入mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<!--引入MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!--阿里的数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--mybatis spring整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring对jdbc整合-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--引入lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<build>
<!-- 配置编译依赖工具 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<!--解决版本问题,发布项目时避免文件丢失-->
<resources>
<!--resources文件-->
<resource>
<directory>src/main/resources</directory>
<!-- 是否被过滤,如果被过滤则无法使用 -->
<filtering>false</filtering>
</resource>
<!--java文件-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
2.编写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: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">
<!--告知spring在创建容器时需要扫描的包,配置所需要的标签不是在beans约束中,
而是一个名称为context名称空间和约束中-->
<context:component-scan base-package="com.accp"></context:component-scan>
<!--读取jdbc.properties-->
<bean id="ppc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--配置数据库连接池-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--配置数据库连接和mybatis的关联-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用数据源组件-->
<property name="dataSource" ref="druidDataSource"></property>
<!--配置sql映射文件信息-->
<property name="mapperLocations" value="classpath*:com/accp/mapper/*.xml"></property>
</bean>
<!--局部配置文件和接口关联配置-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
<property name="basePackage" value="com.accp.mapper"></property>
</bean>
</beans>
3.建立开发目录结构
-
建立实体类Bill
@Data public class Bill { private int id; private String billCode; private String productName; private String productDesc; }
-
建立mapper层
List<Bill> findBill(Bill bill);
<select id="findBill" resultType="com.accp.pojo.Bill"> SELECT b.`id`,b.`billCode`,b.`productName`,b.`productDesc` FROM smbms_bill b <where> <if test="id!=null and id!=''"> id=#{id} </if> <if test="productName!=null and productName!=''"> and productName LIKE CONCAT('%',#{productName},'%') </if> </where> </select>
-
建立service层
@Service public class BillServiceImpl implements BillService { @Autowired private BillMapper billMapper; public List<Bill> findBill(Bill bill){ return billMapper.findBill(bill); } }
-
创建测试类
ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml"); BillServiceImpl billService = ac.getBean("billServiceImpl", BillServiceImpl.class); Bill bill=new Bill(); bill.setId(1); bill.setProductName("皂"); List<Bill> billList = billService.findBill(bill); for (Bill bills : billList) { System.out.println(bills); } ((ClassPathXmlApplicationContext)ac).close();