MyBatis Learning Portal (i)

Source: Internet
Author: User

Jar Package Required: Mybatis-3.x.x.jar, assumptions and spring synthesis, in addition, we need to add the relevant package

1: See the project folder don't mind the red

2: Follow the steps:

1: Add Jar Pack

2: Create a data source (configuration.xml) to 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><!--an alias for the entity class user--><typealias type= "Com.forum.po.User" alias= "User"/></typealiases><!--Data Source configuration this block with BD2 database--><environments default= "development" >< Environment id= "Development" ><transactionmanager type= "jdbc"/><datasource type= "POOLED" >< Property name= "Driver" value= "Com.ibm.db2.jcc.DB2Driver"/><property name= "url" value= "Jdbc:db2://localhost : 50000/forum "/><property name=" username "value=" db2admin "/><property name=" password "value=" admin "/ ></dataSource></environment></environments><mappers><!--usermapper.xml loaded in with equal to the " DAO "Implementation loaded in--><mapper resource=" Mybatis/usermapper.xml "/></mappers></configuration> 
3: Create entity class user

Package com.forum.po;/** * User class *  * @author db2admin *  */public class User extends Entity {private String Name;priv Ate 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 DAO interface, can be said to be mapper interface: Usermapper

Package Com.forum.dao;import Com.forum.po.user;public Interface Usermapper {public User FindByID (String Id);}

5: Create a DAO implementation that differs from Hibernarte's implementation here as an XML file, which is Usermapper.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Mapper Public     "-//mybatis.org//dtd mapper 3.0//en"    "HTTP://MYBATIS.ORG/DTD/MYBATIS-3-MAPPER.DTD" ><!--This is equal to the DAO interface implementation  namespace must be the same as the classpath of the interface--><mapper namespace= "Com.forum.dao.UserMapper" ><!- -FindByID must return a user just like the method name in the interface.  Assuming that the alias is  not aliased to write trouble with the classpath--><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 sqlsessionfactory * sqlsessionfactor Y is responsible for creating the sqlsession once the creation is successful. You can run the mapping statement with the sqlsession instance. 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 sqlsession = Getsessionfactory (). Opensession (); Usermapper usermapper = Sqlsession.getmapper (usermapper.Class); User user = Usermapper.findbyid ("1"); System.out.println (User.getname ());}}

MyBatis Learning Portal (i)

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.