Use of mybatis, use of mybatis

Source: Internet
Author: User

Use of mybatis, use of mybatis

1. Import the jar package

Asm-3.3.1.jar
Cglib-2.2.2.jar
Commons-logging-1.1.1.jar
Log4j-1.2.16.jar
Mybatis-3.1.1.jar
Mysql-connector-java-5.1.10-bin.jar
Slf4j-api-1.6.2.jar
Slf4j-log4j12-1.6.2.jar

 

2. Create a general configuration file

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE configuration 3 PUBLIC "-// mybatis.org//DTD Config 3.0 // EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <typeAliases> 7 <! -- Create an alias for the object class user --> 8 <typeAlias type = "com. kite. mybatis. domain. user "alias =" User "/> 9 </typeAliases> 10 <! -- Configure the data source --> 11 <environments default = "development"> 12 <environment id = "development"> 13 <transactionManager type = "jdbc"/> 14 <dataSource type = "POOLED "> 15 <property name =" driver "value =" com. mysql. jdbc. driver "/> 16 <property name =" url "value =" jdbc: mysql: // localhost: 3306/mybatis "/> 17 <property name =" username "value =" root "/> 18 <property name =" password "value =" aaa123456 "/> 19 </dataSource> 20 </environm Ent> 21 </environments> 22 <! -- The ing file is also the dao implementation class --> 23 <mappers> 24 <mapper resource = "com/kite/mybatis/domain/UserMapper. xml "/> 25 </mappers> 26 </configuration>View Code

3. Create an object class

1 package com. kite. mybatis. domain; 2 3 public class User 4 {5 private String id; 6 private String name; 7 private String age; 8 public String getId () 9 {10 return id; 11} 12 public void setId (String id) 13 {14 this. id = id; 15} 16 public String getName () 17 {18 return name; 19} 20 public void setName (String name) 21 {22 this. name = name; 23} 24 public String getAge () 25 {26 return age; 27} 28 public void setAge (String age) 29 {30 this. age = age; 31} 32 33}View Code

4. Create a dao class

1 package com. kite. mybatis. dao; 2 3 import java. util. list; 4 5 import com. kite. mybatis. domain. user; 6 7 public interface UserMapper 8 {9 public List <User> selectAll (); 10}View Code

5. To create a user ing file as a userMapper implementation, the file header <mapper namespace = "com. kite. mybatis. dao. UserMapper"> must correspond to the dao class.

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE mapper3 PUBLIC "-// mybatis.org//DTD Mapper 3.0 // EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace = "com. kite. mybatis. dao. userMapper "> 6 <select id =" selectAll "resultType =" user "> 7 select * from user8 </select> 9 </mapper>View Code

6. Test

1 package com. kite. mybatis. test; 2 3 import java. io. IOException; 4 import java. util. list; 5 6 import org. apache. ibatis. io. resources; 7 import org. apache. ibatis. session. sqlSession; 8 import org. apache. ibatis. session. sqlSessionFactory; 9 import org. apache. ibatis. session. sqlSessionFactoryBuilder; 10 11 import com. kite. mybatis. dao. userMapper; 12 import com. kite. mybatis. domain. user; 13 14 public class MyBatisTest15 {16 public static void main (String [] args) throws IOException17 {18 // the configuration file 19 SqlSessionFactory factory = new SqlSessionFactoryBuilder () that records mybatis (). build (Resources. getResourceAsReader ("your action. xml "); 20 // create and open sesison21 SqlSession session = factory. openSession (); 22 UserMapper userDao = session. getMapper (UserMapper. class); 23 List <User> userList = userDao. selectAll (); 24 25 System. out. println (userList. size (); 26} 27}View Code

A simple test of mybatis


Use mybatis

Blog is the data object of your database. BlogMapper is used to associate a Blog with a database. It can define some methods, such as the selectBlog () method. The name of this method is selectBlog, be sure to contact your BlogMapper. the IDS of select statements in the xml file are the same, for example, id = "selectBlog", so that no selectBlog method is available.
BlogMapper is a class. You can customize it and put it in the same package as the xml file. It is an interface. You can define a Blog selectBlog (int id, the xml file is written in the same way as ibatis.
In addition, we recommend that you go to the official mybatis website to download documents. Now it seems that there are Chinese characters.
 
Mybatis is now popular, and it seems like a lot of Hibernate is used.

Hibernate is difficult to optimize and configure, but the development efficiency is much higher than mybatis after the framework is set up.
Mybatis is easier to control SQL details, but it is time-consuming to write SQL statements. It is difficult to control the reuse of query SQL statements, resulting in low development efficiency.

Well, there is no difference in the performance between the two, and hibernate also has a sound configuration and method for you to achieve fine-grained control, so there is no reason to say that hibernate is inferior to mybatis in terms of performance, most people will not optimize hibernate in depth, because it is complicated. Hibernate tends to sacrifice some performance to improve development efficiency, while mybatis puts the performance control more directly into your hands, Sacrificing development efficiency.

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.