SSM整合Mybatis-Spring
mybatis -Spring 整合
cn.kitty.bean
public class Book { private int bookid; private String bookname; private int bookprice ; public Book() { } public Book(int bookid, String bookname, int bookprice) { this.bookid = bookid; this.bookname = bookname; this.bookprice = bookprice; }
cn.kitty.dao
public interface IBookDao { public void addBook(Book book); }
cn.kitty.daoi.mpl
<?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="cn.kitty.dao.IBookDao"> <insert id="addBook"> insert book (bookname,bookprice) VALUES (#{bookname},#{bookprice}) </insert> </mapper>
cn.kitty.service
public interface IBookService { public void addBook(Book book); }
cn.kitty.service.impl
package cn.kitty.service.impl; import cn.kitty.bean.Book; import cn.kitty.dao.IBookDao; import cn.kitty.service.IBookService; public class BookServiceImpl implements IBookService { private IBookDao dao; public void addBook(Book book) { dao.addBook(book); } public IBookDao getDao() { return dao; } public void setDao(IBookDao dao) { this.dao = dao; } }
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" 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.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 "> <!--01.识别jdbc.properties文件--> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql:///aount"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value=""></property> </bean> <!--03.工厂配置--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--big config path--> <property name="configLocation" value="classpath:Mybatis-config.xml"></property> </bean> <!--dao 实现类 映射文件的扫描器可以动态的在内存中构建接口的实现类,代理对象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.kitty.dao"></property> </bean> <!--service--> <bean id="bookService" class="cn.kitty.service.impl.BookServiceImpl"> <property name="dao" ref="IBookDao"></property> </bean> <!--06.事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 07.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> <!--配置了切点Pointcut * *..service.*.*(..))--> <aop:pointcut id="mypoint" expression="execution(* *..service.*(..))"/> <!--顾问--> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor> </aop:config> </beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///aount jdbc.username=root jdbc.password=
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.kitty.bean"></package> </typeAliases>--> <mappers> <!--<package name="cn.kitty.dao"></package>--> <mapper resource="cn/kitty/dao/IBookDao.xml"/> </mappers> </configuration>
Test测试
package cn; import cn.kitty.bean.Book; import cn.kitty.service.IBookService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSSM { @Test public void ssm(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml"); IBookService bookService =(IBookService)context.getBean("bookService"); Book book=new Book(); book.setBookname("mybatis-Spring-"); book.setBookprice(23); bookService.addBook(book); } }
运行结果