Mybatis getting started

Source: Internet
Author: User

Required jar package: mybatis-3.x.x.jar, if you need to integrate with spring, also need to add the relevant package

1: do not care about the red color of the project directory.

2:Follow these steps:

1: Add a jar package

2: create a data source (configuration. XML) Create a database (omitted here)

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype configuration public "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typealiases> <! -- Create an alias for the object class user --> <typealias type = "com. Forum. Po. User" alias = "user"/> </typealiases> <! -- Configure the data source in MySQL database --> <environments default = "development"> <environment id = "development"> <transactionmanager type = "JDBC"/> <datasource type = "pooled "> <property name =" driver "value =" com. mySQL. JDBC. driver "/> <property name =" url "value =" JDBC: mysql: // localhost: 3306/test "/> <property name =" username "value =" test "/> <property name =" password "value =" test "/> </datasource> </ environment> </environments> <mapper S> <! -- When usermapper. XML is loaded, the "Dao" implementation is loaded. --> <mapper resource = "mybatis/usermapper. xml"/> </mappers> </configuration>

3: Create object class user
Package COM. forum. po;/*** user class **/public class user extends entity {private string name; private integer age; Public String getname () {return name ;} public void setname (string name) {This. name = Name;} public integer getage () {return age;} public void setage (integer age) {This. age = age;} public user (){}}

4. Create the DaO interface, which can be called the Mapper interface: usermapper
package com.forum.dao;import com.forum.po.User;public interface UserMapper {public User findById(String Id);}

5: The implementation of creating Dao is different from the implementation of hibernarte here as an XML file, that is, usermapper. 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 = "com. Forum. 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 butone. student where ID =#{ ID} </SELECT> </mapper>

6: Test class, mybatistest
Package COM. forum. test; import Java. io. ioexception; 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 COM. forum. dao. usermapper; import COM. forum. po. user;/*** mybatis database connection test ** @ author db2admin **/public class mybatistest {/*** get mybatis sqlsessionfacto Ry * sqlsessionfactory is responsible for creating sqlsession. Once created successfully, you can use the sqlsession instance to execute ing statements, commit, rollback, close, and other methods. * @ Return */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 blocke. printstacktrace ();} return sessionfactory;} public static void main (string [] ARGs) {sqlsession = getsessionfactory (). opensession (); usermapper = sqlsession. getmapper (usermapper. class); User user = usermapper. findbyid ("1"); system. out. println (user. getname ());}}

Mybatis getting started

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.