springframework 4.3.9 mybatis 3.4.4 框架整合
--------------------------------------------------------
1、数据库准备 数据库信息
db
root
root
3306
localhost
mysql> select * from book;
+---------+-----------------------------+------------+
| book_id | book_name | book_price |
+---------+-----------------------------+------------+
| 2 | 《java入门与精通》 | 355.00 |
| 3 | 《MySQL入门与精通》 | 105.00 |
| 8 | 《MySQL入门与精通》 | 105.00 |
| 9 | 《Html5入门与精通》 | 105.00 |
| 10 | 《css入门与精通》 | 205.00 |
| 11 | 《综合项目》 | 105.00 |
| 16 | 《数据库优化案例》 | 80.00 |
+---------+-----------------------------+------------+
7 rows in set (0.02 sec)
mysql> desc book;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| book_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| book_name | varchar(20) | YES | | NULL | |
| book_price | decimal(8,2) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
2、建立maven项目pom.xml 配置如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fz</groupId>
<artifactId>springmybatis</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.8.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<testSourceDirectory>src/test/java</testSourceDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<!-- 处理无法加载资源配置文件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
3、在src/main/resources/db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8&useSSL=true
db.user=root
db.password=root
在src/main/resources/mybatis-config.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>
<package name="com.fz.entity"/>
</typeAliases>
<!-- 实体接口映射资源 -->
<!--
说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml
-->
<mappers>
</mappers>
</configuration>
在src/main/resources/beans.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.fz.entity"/>
<bean id="dd" class="java.util.Date"/>
<!-- 引入配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"/>
</bean>
<!-- 使用spring jdbc中的类建立数据源 -->
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.password}"/>
</bean>
<!-- SqlSessionFactory -->
<bean id="sf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.fz.entity"/>
<property name="mapperLocations" value="classpath:com/fz/mapper/*Mapper.xml"/>
</bean>
<!-- SqlSession -->
<bean id="session" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sf" />
</bean>
<!--创建数据映射器,数据映射器必须为接口 -->
<!--
<bean id="ssm" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.entity.mapper.StudentMapper" />
<property name="sqlSessionFactory" ref="sf" />
</bean>
-->
<!-- 扫描mapper包 自动在spring中生成 xxxxMapper bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fz.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sf"/>
</bean>
<bean id="txm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 使用声明式事务 transaction-manager:引用上面定义的事务管理器 -->
<tx:annotation-driven transaction-manager="txm" />
</beans>
4、编写模型类,实体类
src/main/java/com/fz/entity/Book.java
package com.fz.entity;
import lombok.Data;
/**
* Created by webrx on 2017-07-05.
*/
@Data
public class Book {
private int id;
private String name;
private double price;
}
5、编写mybatis mapper接口BookMapper.java及配置文件BookMapper.xml
src/main/java/com/fz/mapper/BookMapper.java
package com.fz.mapper;
import com.fz.entity.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created by webrx on 2017-07-06.
*/
@Component("bd") @Transactional
public interface BookMapper {
public List<Book> query();
@Delete("delete from book where book_id=#{id}")
public int delById(int id);
@Select("select * from book where book_id=#{id}") @ResultMap("bookmap")
public Book queryById(int id);
@Select("select book_name from book")
public List<String> queryName();
}
src/main/java/com/fz/mapper/BookMapper.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.fz.mapper.BookMapper">
<resultMap id="bookmap" type="book">
<id property="id" column="book_id"/>
<result property="name" column="book_name"/>
<result property="price" column="book_price"/>
</resultMap>
<select id="query" resultMap="bookmap">
select * from book
</select>
</mapper>
6、编写测试类 src/test/Demo.java
import com.fz.entity.Book;
import com.fz.mapper.BookMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
/**
* Created by webrx on 2017-07-06.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:beans.xml")
public class Demo {
@Autowired
protected DriverManagerDataSource ds;
@Autowired
protected Date dd;
@Autowired
protected BookMapper bookdao;
//@Resource(name="bd")
//protected BookMapper bookdao;
//@Autowired
//@Qualifier("bd")
//protected BookMapper bookdao;
@Test
public void jdbc() throws SQLException {
Connection conn = ds.getConnection();
System.out.println(conn);
}
@Test
public void aa(){
System.out.println(dd);
//int rr = bookdao.delById(12);
//System.out.println(rr);
//List<Book> bks = bookdao.query();
//System.out.println(bks.size());
//for(Book b : bks){
// System.out.println(b.getName());
//}
//Book book = bookdao.queryById(9);
//System.out.println(book);
List<String> bnames = bookdao.queryName();
System.out.println(bnames.size());
for(String n : bnames){
System.out.println(n);
}
}
}