Spring与MyBatis整合
Posted 清风徐来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring与MyBatis整合相关的知识,希望对你有一定的参考价值。
1、Spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
2、MyBatis
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
1.Maven引入jar包
<!--druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency> <!--MyBatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!--单测--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.3</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!--mybatis jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!--Mybatis+Spring整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.1</version> </dependency> <!-- Spring整合JavaWeb的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!--jstl表达式--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--java ee--> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <!-- mysqljar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--aop使用的jar--> <dependency> <groupId> org.aspectj</groupId > <artifactId> aspectjweaver</artifactId > <version> 1.8.7</version > </dependency>
2.创建JDBC.properties文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/y2167?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=
3.创建Spring-MyBatis.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" 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"> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--识别jdbc.properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--把SqlSessionFactory生产权交给spring--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <!--DAO--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--设置包路径--> <property name="basePackage" value="cn.happy.day16SSM.dao"/> </bean> <!--service--> <bean id="bookservice" class="cn.happy.day16SSM.service.BookServiceImpl">
<!--注意事项:如果接口是以I开头,那么会默认在内存中默认生成一个与接口名字一样的实现类如IXXXDAO
如果不是一I开头,则默认生成第一个小写字母开头如 xXXDAO
--> <property name="dao" ref="IBookDAO"/> </bean> </beans>
4.数据库结构
4.创建接口实体类
代码如下
package cn.happy.day16SSM.dao; import cn.happy.day16SSM.entity.Book; /** * Created by Administrator on 2018/3/16. */ public interface IBookDAO { //添加图书 public int addBook(Book book); }
DAO.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"> <!--namespace:接口绝对路径--> <mapper namespace="cn.happy.day16SSM.dao.IBookDAO"> <!--根据Id修改Name--> <insert id="addBook"> INSERT into book(bookName,bookAuthor,bookPrice)VALUES (#{bookName},#{bookAuthor},#{bookPrice}) </insert> </mapper>
entity
package cn.happy.day16SSM.entity; /** * Created by Administrator on 2018/3/16. */ public class Book { private Integer bookId; private String bookName; private String bookAuthor; 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 String getBookAuthor() { return bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } public Integer getBookPrice() { return bookPrice; } public void setBookPrice(Integer bookPrice) { this.bookPrice = bookPrice; } }
serviceImpl
package cn.happy.day16SSM.service; import cn.happy.day16SSM.dao.IBookDAO; import cn.happy.day16SSM.entity.Book; /** * Created by Administrator on 2018/3/16. */ public class BookServiceImpl implements BookService { // private IBookDAO dao; public IBookDAO getDao() { return dao; } public void setDao(IBookDAO dao) { this.dao = dao; } public int addBook(Book book) { return dao.addBook(book); } }
5.测试类
package day16SSM; import cn.happy.day15tx.service.IStockService; import cn.happy.day16SSM.dao.IBookDAO; import cn.happy.day16SSM.entity.Book; import cn.happy.day16SSM.service.BookService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by Administrator on 2018/3/3. */ public class Test20180316 { //Spring+MyBatis整合 @Test public void Spring(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("Spring-MyBatis.xml"); BookService service=(BookService)ctx.getBean("bookservice"); Book book=new Book(); book.setBookName("JAVA Web"); book.setBookAuthor("许令波"); book.setBookPrice(60); service.addBook(book); } }
测试结果
以上是关于Spring与MyBatis整合的主要内容,如果未能解决你的问题,请参考以下文章