Spring and MyBatis Environment integration

Source: Internet
Author: User

The SSH framework is almost household, but the general medium and small projects, using spring and MyBatis is enough, and mybatis light, easy to use, it is worth trying once.

Introduction:

Spring:

Spring is a lightweight control inversion (IoC) and facet-oriented (AOP) container framework.

Two important modules: Spring aspect-oriented programming (AOP) and control inversion (IOC) containers.

The basic concept of control reversal mode (also known as dependency intervention) is that you do not create objects, but describe how they are created. The code does not directly connect to objects and services, but describes in the configuration file which component requires which service.

Containers (which are IOC containers in the Spring framework) are responsible for linking these together. In a typical IOC scenario, the container creates all the objects and sets the necessary properties to connect them together, deciding when to invoke the method.

MyBatis:

    

MyBatis is an excellent persistence layer framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual setting of almost all JDBC code and parameters and the retrieval of the result set. 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.

Start integrating spring and MyBatis:

1. Create a new Web project in Eclipse and import the jar packages we need, which can be downloaded here: http://pan.baidu.com/s/1qW8Y6yo

Jar List:

  

2. Create a simple database (Springmybaitis) and data table (user---id,username,password).

  

3, take the user management as an example, create model and DAO.

The file list is as follows:

  

Model----User.java

  

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

 

DAO------Interface Userdao.java

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

 

DAO-----Userdao.xml

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

4. Spring configuration file----Applicationcontext.xml

  

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

5. MyBatis Configuration file------Mybatis-config.xml

  

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

6. Testing

First Test add a piece of data:

  

 1 package com.springMyBatis.system.test; 2 3 Import Org.springframework.context.ApplicationContext; 4 Import Org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 Import Com.springMyBatis.system.dao.UserDao; 7 Import Com.springMyBatis.system.model.User; 8 9 public class Usercontroller {Ten/**12 * @param args13 */14 public static void main (string[] args) {ApplicationContext ctx=null;16 ctx=new classpathxmlapplicationcontext ("Applicationcontext.xml");         Userdao userdao= (Userdao) Ctx.getbean ("Userdao"); user user=new User (); 19//Add two data 20 User.setid (1); User.setusername ("Jessica"); User.setpassword ("123"); Userdao.adduser (user); 2 4 User.setid (2); User.setusername ("JESSICA2"); User.setpassword ("123"); Userdao.addu         Ser (user), System.out.println ("Add Success"), 29//Query Data User.setusername ("Jessica"); 31User.setpassword ("123"), System.out.println (Userdao.getuser (user). ToString ()), User.setusername ("Jessi Ca2 "), User.setpassword (" 123 "), System.out.println (Userdao.getuser (user). ToString ()); 36//Modify Data PNS User.setid (2), User.setpassword ("802"), Userdao.updateuser (user), System.out.prin TLN ("modified successfully"); 41//delete data Userdao.deleteuser (1); System.out.println ("delete succeeded"); 44 45}46 47}

  

There is only one data left in the last database:

  

The combination of spring and mybatis is simple, and for some smaller projects, to be refined and manageable, the two frameworks are combined enough. Although now hibernate is also very red, but feel it inside is very mysterious, difficult to configure, also not good optimization, is a kind of dare not to start the feeling. Although hibernate configuration is a bit difficult, but after a good environment, the development efficiency is still very high, only need to configure the table relationship or something OK. But it's hard to control the details, for example, by looking up a data or something. MyBatis can control the details of SQL, although writing SQL statements is time-consuming and requires your SQL foundation, but also because of this, the use of MyBatis people's SQL level will improve Ah, see our project manager two or three minutes to make a seemingly complex but efficient SQL statement, I feel very handsome and very bull-like. So analysis, the two can not be thrown together to learn a bit. This article is just a simple example, in the *dao.xml configuration file, there are other help to optimize and simplify SQL, to help improve the efficiency of some knowledge, we need to learn further. Also, you can install DAO by combining spring automatic scanning, or you can reduce the burden of configuration files and look forward to the next more mature use.

Spring and MyBatis Environment 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.