Simple Example of Mybatis and simple example of Mybatis

Source: Internet
Author: User

Simple Example of Mybatis and simple example of Mybatis

First, create a JavaWeb project and import the jar package on which mybatis depends. At the same time, Mybatis performs database operations. Therefore, we need to create a new table user in the database for demonstration.

After creating a table, we also need to create the corresponding object class User. java and add the set and get methods:

 1 public class User { 2     private String username; 3     private String password; 4     private int age; 5     public String getUsername() { 6         return username; 7     } 8     public void setUsername(String username) { 9         this.username = username;10     }11     public String getPassword() {12         return password;13     }14     public void setPassword(String password) {15         this.password = password;16     }17     public int getAge() {18         return age;19     }20     public void setAge(int age) {21         this.age = age;22     }23 }

In Mybatis, we need to create a ing file userMapper. xml corresponding to the object class:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> 3 <! -- Specify a unique namespace for this mapper. Set the namespace value to package name + SQL ing file name) 4 --> 5 <mapper namespace = "com. mybatis. mapping. userMapper "> 6 <! -- Write the query SQL statement in the select tag. The id attribute value must be unique. 7. Use the parameterType attribute to specify the parameter type used for the query, the resultType attribute specifies the returned result set type 8 --> 9 <! -- 10 get a user object 11 According to username query --> 12 <select id = "getUser" parameterType = "java. lang. string "13 resultType =" com. mybatis. po. user "> 14 select * from user where username =#{ username} 15 </select> 16 17 <delete id =" deleteUser "parameterType =" java. lang. string "> 18 delete from user where username =#{ username} 19 </delete> 20 </mapper>

Finally, we need to create a configuration file config. xml for Mybatis to connect to the database under src, and import the above userMapper. xml. The Code is as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 // EN" http://mybatis.org/dtd/mybatis-3-config.dtd "> 3 <configuration> 4 <environments default =" development "> 5 <environment id =" development "> 6 <transactionManager type = "JDBC"/> 7 <! -- Configure database connection information --> 8 <dataSource type = "POOLED"> 9 <property name = "driver" value = "com. mysql. jdbc. driver "/> 10 <property name =" url "value =" jdbc: mysql: // localhost: 3306/test "/> 11 <property name =" username "value =" root "/> 12 <property name =" password "value =" "/> 13 </dataSource> 14 </environment> 15 </environments> 16 <mappers> 17 <! -- Register userMapper. xml file. resource is userMapper. xml directory --> 18 <mapper resource = "com/mybatis/mapping/userMapper. xml "/> 19 </mappers> 20 </configuration>

The configuration of database connection information here is not very different from that of Hibernate. Now we create a Test class to Test it:

1 public class Test {2 3 public static void main (String [] args) throws IOException {4 // configuration file of mybatis 5 String resource = "config. xml "; 6 // use the class loader to load the configuration file of mybatis (It also loads the associated ing file) 7 InputStream is = Test. class. getClassLoader (). getResourceAsStream (resource); 8 // sqlSession factory 9 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder (). build (is); 10 // open session11 SqlSession session = sessionFactory. openSession (); 12/** 13 * ing the SQL ID string 14 * com. mybatis. mapping. userMapper is userMapper. the namespace attribute value of the ER er tag in the xml file. 15 * getUser is the id attribute value of the select tag, you can find the SQL 16 */17 String statement = "com. mybatis. mapping. userMapper. getUser "; // ing the SQL ID string 18 // execute the query and return the sql19 user User = session of a unique user object. selectOne (statement, "username1"); 20 System. out. println (user. getUsername (); 21 String statement2 = "com. mybatis. mapping. userMapper. deleteUser "; 22 session. delete (statement2, user); 23} 24}

Executing the selectOne method will return a user object (if you want to query multiple pieces of data, you can use selectList, this method will return the List <User> Object), We output the username of the user object on the console. executing the delete method can directly delete a piece of data corresponding to the object, and determine whether the execution is successful based on changes in the database. The following is the directory of my project. For details, refer:

The above is a simple example of Mybatis. Of course, in userMapper. xml, we can also use OGNL to generate dynamic SQL statements. If you are interested, you can study it yourself.

Related Article

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.