SSM spring-mybatis整合

我的spring-mybatis整合使用添加book的列子

一、首先创建了实体类Book

package cn.sun.day15ssm.entity;

/**
* Created by Administrator on 2017/10/21.
*/
public class Book {
private Integer bookId;
private String bookName;
private Integer bookPrice;
public Integer getBookId() {
return bookId;
}

public void setBookId(Integer bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public Integer getBookPrice() {
return bookPrice;
}

public void setBookPrice(Integer bookPrice) {
this.bookPrice = bookPrice;
}

}
封装了bookId,bookName,和bookPrice,
创建dao层
创建一个bookDAO的接口,在接口中创建一个addBook的方法
package cn.sun.day15ssm.dao;

import cn.sun.day15ssm.entity.Book;

/**
* Created by Administrator on 2017/10/21.
*/
public interface IBookDAO {
public int addBook(Book book);
}
创建一个IBookDAO 的xml文件,在其中实现sql语句
<?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">
<!--映射文件的根节点
namespace
-->
<!--映射文件的根节点 namespace -->
<mapper namespace="cn.sun.day15ssm.dao.IBookDAO">
<insert id="addBook"> insert into book(bookName,bookPrice) values(#{bookName},#{bookPrice}) </insert>
</mapper>
注意属性要与数据库和实体类的名字一样
创建book的service层中的接口
package cn.sun.day15ssm.service;

import cn.sun.day15ssm.entity.Book;

/**
* Created by Administrator on 2017/10/21.
*/
public interface IBookService {
public int addBook(Book book);
}
创建实现类BookServiceImpl
package cn.sun.day15ssm.service.impl;

import cn.sun.day15ssm.dao.IBookDAO;
import cn.sun.day15ssm.entity.Book;
import cn.sun.day15ssm.service.IBookService;

/**
* Created by Administrator on 2017/10/21.
*/
public class BookServiceImpl implements IBookService {
private IBookDAO dao;

public int addBook(Book book) {
return dao.addBook(book);
}
public IBookDAO getDao() {
return dao;
}

public void setDao(IBookDAO dao) {
this.dao = dao;
}
}
二、然后,在resources中创建jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///book
jdbc.username=root
jdbc.password=123456
创建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="cn.sun.day15ssm.entity"></package>
</typeAliases>
<!--管理小配置-->
<mappers>
<package name="cn.sun.day15ssm.dao"></package>
</mappers>

</configuration>
创建applicationContextday15ssm.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--2.Druid数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--mybatis sqlsessionFactory配置-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--dao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.sun.day15ssm.dao"></property>
</bean>
<!--service-->
<bean id="bookService" class="cn.sun.day15ssm.service.impl.BookServiceImpl">
<property name="dao" ref="IBookDAO"></property>
</bean>
<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入什么东西 事务对象的来源,一定是Connection -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--方式三,AspectJ AOP事物管理机制-->
<!--<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="addBook" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
&lt;!&ndash;切点&ndash;&gt;
<aop:pointcut id="mypointcut" expression="execution(* *..day15ssm.service.*.*(..))"></aop:pointcut>
&lt;!&ndash;顾问&ndash;&gt;
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"></aop:advisor>
&lt;!&ndash;切面&ndash;&gt;
</aop:config>-->
</beans>
其中由于dao层中BookDAO没有实现类所以我们使用了一个org.mybatis.spring.mapper.MapperScannerConfigurer ,mybatis-spring jar中的类使用
basePackage来给dao模拟出一个实现类
三、pom文件的配置
<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/maven-v4_0_0.xsd">
<parent>
<artifactId>01Mybatis</artifactId>
<groupId>cn.sun</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>03Spring</artifactId>
<packaging>war</packaging>
<name>03Spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>

<!--织入的包-->
<!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver -->
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency>

<!--spring JDBCTemplate-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

<!--dbcp 数据源-->
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>

<!--c3p0-->
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

<!--alibaba datasource-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>

<!--mybatis jar-->

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

<!--spring整合mybatis-->
<!--Mybatis+Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>

</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>

四、最后我创建了一个测试类来测试一下

package cn.sun.day01;

import cn.sun.day15ssm.entity.Book;
import cn.sun.day15ssm.service.IBookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Created by Administrator on 2017/10/21.
*/
public class Test20171021 {
@Test
// 01.事务
public void test01(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextday15ssm.xml");
IBookService service= (IBookService)ctx.getBean("bookService");
Book book=new Book();
book.setBookName("挪威的森林");
book.setBookPrice(200);
service.addBook(book);
}
}执行成功,在数据库中可以查到我们刚才添加进来的内容,整合成功!

posted on 2017-10-21 15:12  灬疯狂的萝卜  阅读(189)  评论(0编辑  收藏  举报

导航