Spring and MyBatis Integration

Source: Internet
Author: User
Tags aop

1. Spring

Spring is an open source framework, and spring is a lightweight Java development framework that emerged in 2003 by Rod Johnson in his book expert one-on-one development and Some of the concepts and prototypes elaborated in design are derived. It is created to address the complexities of enterprise application development. Spring uses basic JavaBean to accomplish things that were previously only possible by EJBS. However, the use of spring is not limited to server-side development. From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring. In short, spring is a lightweight control inversion (IoC) and aspect-oriented (AOP) container framework.

2, MyBatis

MyBatis is an open source project for Apache Ibatis, which was migrated to Google code by the Apache Software Foundation in 2010 and renamed MyBatis. MyBatis is a Java-based persistence layer framework. The persistence layer framework provided by Ibatis includes SQL maps and Data Access Objects (DAO) MyBatis eliminates the manual setting of almost all JDBC code and parameters and the retrieval of result sets. MyBatis uses simple XML or annotations for configuration and raw mapping, mapping interfaces and Java POJOs (Plain old Java Objects, ordinary Java objects) to records in a database.

1.Maven introduction of JAR Packages

<!--druid Data source-<dependency> <groupId>com.alibaba</groupId> <artif actid>druid</artifactid> <version>1.0.29</version> </dependency> <! --mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactid& Gt;mybatis</artifactid> <version>3.2.2</version> </dependency> <!--single test- <dependency> <groupId>junit</groupId> <artifactid>junit</art ifactid> <version>4.3</version> <scope>test</scope> </dependenc            Y> <!--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.spring Framework</groupid> <artifactId>spring-context</artifactId> &LT;VERSION&GT;4.2.0.R Elease</version> </dependency> <!--mybatis Jar Pack---<dependency> &L T;groupid>org.mybatis</groupid> <artifactId>mybatis</artifactId> <version&gt            ;3.2.2</version> </dependency> <!--mybatis+spring Integration--<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> &lt ;version>1.2.1</version> </dependency> <!--Spring Integrated Javaweb Pack-and-<dependency > <groupId>org.springframework</groupId> <artifactid>spring-web</artifactid&            Gt <versiOn>4.2.0.release</version> </dependency> <!--jstl Expressions---<dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1 .2</version> </dependency> <!--java ee--> <dependency> <groupid >javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</vers Ion> </dependency> <!--mysqljar--<dependency> <groupid>mysq L</groupid> <artifactId>mysql-connector-java</artifactId> <version>5.1.38&lt ;/version> </dependency> <!--AOP uses jar--> <dependency> <groupid&gt ; Org.aspectj</groupid > <artifactId> aspectjweaver</artifactid > <version> 1.  8.7</version >      </dependency> 

  

2. Create a jdbc.properties file

jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/y2167?useunicode=true& characterencoding=utf8jdbc.username=rootjdbc.password=

  

3. Create a Spring-mybatis.xml file

<?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 "> <!--configuration data source-<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=" ${jd Bc.username} "/> <property name=" password "value=" ${jdbc.password} "/> </bean> <!--recognition Jdbc.pro Perties file--<context:property-placeholder location= "Classpath:jdbc.properties"/> <!--give sqlsessionfactory production rights to spring--> <bean class= "Org.mybatis.spring.SqlSessionFactoryBean" > <propert Y name= "DataSource" ref= "DataSource"/> </bean> <!--dao--> <bean class= "Org.mybatis.spring.mappe R.mapperscannerconfigurer "> <!--Set package path--<property name=" Basepackage "value=" CN.HAPPY.DAY16SSM.D Ao "/> </bean> <!--service--> <bean id=" Bookservice "class=" Cn.happy.day16SSM.service.BookServic Eimpl ">
<!--NOTE: If the interface starts with I, it defaults to generating an implementation class with the same name as the interface by default in memory, such as Ixxxdao
If it is not a start, the default is to generate the first lowercase letter beginning as Xxxdao
--<property name= "DAO" ref= "Ibookdao"/> </bean></beans>

 

4. Database structure

4. Creating an Interface entity class

 

The code is as follows

Package Cn.happy.day16ssm.dao;import cn.happy.day16ssm.entity.book;/** * Created by Administrator on 2018/3/16. */public interface Ibookdao {    //Add library public    int Addbook (books 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: interface absolute path--><mapper namespace= "Cn.happy.day16SSM.dao.IBookDAO" >    <!-- Modify name-->    <insert id= "Addbook" > INSERT INTO Book        (Bookname,bookauthor,bookprice) VALUES by ID (#{ 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. Test class

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 integration    @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 ("Xilingpo");        Book.setbookprice ();        Service.addbook (book);    }}

  

Test results

Spring and MyBatis integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.