Mybatis Study Notes-1

Source: Internet
Author: User

Mybatis Is An ORM framework. activiti uses mybatis, so I will learn it;

Mybatis can run independently without any framework or Tomcat;

1. steps:

1. manually create a database table and create a user table;

2. Create the core configuration file configuration. xml;

3. Create a pojo object file: User. Java;

4. Create Dao: interface usermapper. Java, interface implementation usermapper. xml;

5. Create a test class;

2. steps:

Overview project file directory:

1. Create a user table (using the H2 database and embedded database for testing );

// Create table user (ID int identity () primary key, username varchar (10); // Insert the initial user insert into user (username) values ('user 1'); insert into user (username) values ('user 2'); insert into user (username) values ('user 3 ');

A. Connect to the H2 database. The jar package already contains the driver:

B. The created user table:

2. configuration. xml configuration file:

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype configuration public "-// mybatis.org//dtd config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Load External database connection information file --> <properties resource = "DB. properties"/> <typealiases> <! -- Create an alias user for the object class, which is used to replace the full path name in the ing file and simplify writing --> <typealias type = "test. java. pojo. user "alias =" user "/> </typealiases> <! -- Configure the data source to use the H2 database connection information in the DB. properties --> <environments default = "development"> <environment id = "development"> <transactionmanager type = "JDBC"/> <datasource type = "pooled"> <property name = "driver" value = "$ {dB. driver} "/> <property name =" url "value =" $ {dB. URL} "/> <property name =" username "value =" $ {dB. username} "/> <property name =" password "value =" $ {dB. password} "/> </datasource> </environment> </enviro Nments> <mappers> <! -- Usermapper. loading XML is equivalent to loading the "Dao" implementation --> <mapper resource = "test/Java/Dao/usermapper. XML "/> </mappers> </configuration>

3. create user. Java:

package test.java.pojo;public class User {        private int id;private String username;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}}

4. Create Dao (add, delete, modify, and query) and DAO implementation

Dao interface: adds, deletes, modifies, and queries, and implements the idea of interface-oriented programming. resultmap is the result ing corresponding to the database fields, which allows you to easily query the result values.

Package test. java. dao; import Java. util. list; public interface usermapper {// add or delete a common query public user findbyid (int id); public user findbyname (string username); public list <user> findall (); public int insert (User user); Public int Delete (string username); Public int Update (User user); Public int countall ();}

DAO implementation: mybatis implementation method. Input SQL statements in XML.

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- The namespace equivalent to the DaO interface must be the same as the class path of the interface --> <mapper namespace = "test. java. Dao. usermapper"> <! -- The findbyid must be the same as the method name in the interface. Returning a user is the alias just now. If you do not get the alias, you need to write it together with the class path. --> <select id = "findbyid" parametertype = "hashmap" resulttype = "user"> select * from user where id = # {ID} </SELECT> <select id = "findbyname" parametertype = "string" resulttype = "user"> select * from user where username =#{ username} </SELECT> <select id = "findall" resultmap = "userresultmap"> select * from user </SELECT> <resultmap type = "User "id =" userresultmap "> <ID property =" ID "column =" ID "/> <result property =" username "column =" username "/> </resultmap> <insert id = "insert" parametertype = "user"> insert into user values (# {ID }, # {username }) </Insert> <Delete id = "delete" parametertype = "string"> Delete from user where username like # {username} </delete> <update id = "Update" parametertype = "user"> Update user set username =#{ username} Where ID =#{ ID} </update> <select id = "countall" resulttype = "int"> select count (1) from user </SELECT> </mapper>

5. Test class

Package test. java. mytest; import Java. io. ioexception; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import Org. apache. ibatis. annotations. resultmap; import Org. apache. ibatis. io. resources; import Org. apache. ibatis. session. sqlsession; import Org. apache. ibatis. session. sqlsessionfactory; import Org. apache. ibatis. session. sqlsessionfactorybuilder; import test. java. dao. usermapper; import test. java. pojo. user; public class mybatistest {public static void main (string [] ARGs) {sqlsession = getsessionfactory (). opensession (); usermapper = sqlsession. getmapper (usermapper. class); system. out. println (usermapper. countall (); User user = new user (); User. setid (2); User. setusername ("text 7"); // usermapper. insert (User); usermapper. update (User); system. out. println (usermapper. countall (); // system. out. println (usermapper. findbyname (user. getUserName ()). GETID (); // system. out. println (usermapper. findall (). get (1 ). getUserName (); // list <user> User = usermapper. findall (); // iterator <user> it = user. iterator (); // while (it. hasnext () {// User Users = it. next (); // system. out. println (users); //} // user = usermapper. findbyid (2); // system. out. println (user. getUserName (); sqlsession. commit (); sqlsession. close ();} // session factory Private Static sqlsessionfactory getsessionfactory () {sqlsessionfactory sessionfactory = NULL; string resource = "configuration. XML "; try {sessionfactory = new sqlsessionfactorybuilder (). build (resources. getresourceasreader (Resource);} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();} return sessionfactory ;}}

Iii. Summary

Mybatis runs like a graphical link:

Mybatis Study Notes-1

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.