Spring整合MyBatis

xiaoxiao2021-02-28  102

首先下载jar包  mybatis-spring.jar  

原因spring3.0出来的早,MyBatis3.0晚,意味着Spring不愿意去在一个没有做出发布版本的MyBatis上做过多的设置。所以,最终jar包提供者第三方。

 <!--Mybatis+Spring整合-->

    <dependency>

      <groupId>org.mybatis</groupId>

      <artifactId>mybatis-spring</artifactId>

      <version>1.2.0</version>

    </dependency>0.jar包  mybatis-spring.jar  

先建一个SpringSSM文件,在该文件中创建实体类entity层,并在该层中写上和数据库相应的属性值,并将其进行get,set封装

添加图书分层:

public class Book { private Integer bookId; private String bookName; private int bookPrice; 创建dao层,在该层中创建BookDao接口,并写上实现方法 public interface BookDao { //添加 public int add(Book book); } 写完接口之后,接着在该层中写上一个小配置BookDao.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"> <mapper namespace="cn.happy.SpringSSM.dao.BookDao"> <insert id="add"> insert into book(bookname,bookprice) values(#{bookName},#{bookPrice}) </insert> </mapper>接下来就是Service层了,同样在该层中创建一个BookService接口,并写上一个实现方法,和dao层中的方法一样public interface BookService { public int add(Book book); }当然也要在该层中创建一个BookServiceImpl类,用它来实现接口中的方法,并且将dao层中的接口名称植入到该类中  public class BookServiceImpl implements BookService {

BookDao dao; public BookDao getDao() { return dao; } public void setDao(BookDao dao) { this.dao = dao; } public int add(Book book) { return dao.add(book); } } 接下来最重要的就是resources中配置了

首先创建一个jdbc.properties,在这写上连接数据库的用户名,密码,数据库名等等 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///bookstok jdbc.user=sha jdbc.password=sha 还有一个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.happy.SpringSSM.entity"></package> </typeAliases> </configuration>之后就是大配置了<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 1.识别jdbc。properties--> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--2.dbcp数据源--> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///bookstok"></property> <property name="username" value="sha"></property> <property name="password" value="sha"></property> </bean>--> <!--2.创建数据源 Spring--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.user}" ></property> <property name="password" value="${jdbc.password}" ></property> </bean> <!--3.配置SqlSessionFactoryBean 工厂配置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--引用数据源组件--> <property name="dataSource" ref="dataSource"></property> <!--引用mybatis配置文件中的配置--> <property name="configLocation" value="classpath:MyBatis-config.xml"></property> </bean><!--4.dao 实现类,映射文件的扫描可以动态的在内存中构建接口的实现类,代理对象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.happy.SpringSSM.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean> <!--5.service--> <bean id="bookservice" class="cn.happy.SpringSSM.service.BookServiceImpl"> <property name="dao" ref="bookDao"></property> </bean><!--6.事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 7.AspectJ AOP 配置事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="buy*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="StockException"/> </tx:attributes> </tx:advice> <aop:config> <!--配置了切点Pointcut--> <aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/> <!--顾问--> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor> </aop:config></beans>接下来就到测试类了public class SpringSSMText { @Test public void ssmtest(){ ApplicationContext appl=new ClassPathXmlApplicationContext("applicationContextSpringSSM.xml"); BookService bb= (BookService) appl.getBean("bookservice"); Book bk=new Book(); bk.setBookName("12"); bk.setBookPrice(23); bb.add(bk); } }-----------------------------------------------------------------------------------------
转载请注明原文地址: https://www.6miu.com/read-62255.html

最新回复(0)