MyBatis Study Notes First lecture

Source: Internet
Author: User

First step: Configure the MyBatis configuration first

<?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> <typealias alias= "Users" type= "ORM. Users "/> </typeAliases> <environments default=" Development "> <environment id=" Development "> <transactionmanager type=" JDBC "/> <datasource type=" pooled "> <property n Ame= "Driver" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://127.0.0.1:3306/myba Tis?characterencoding=utf-8 "/> <property name=" username "value=" root "/> <property name=        "Password" value= "123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource= "Orm/users.xml"/> </mappers></configuration>

Step two: Create an Entity object

Package Orm;public class Users {private Integer id;private string username;private string password;private Integer age;pri vate integer sex;public Users (integer ID, string username, string password, integer age,integer sex) {super (); this.id = ID ; this.username = Username;this.password = Password;this.age = Age;this.sex = sex;} You must add a non-parametric construct public Users () {}//set and get omitted @overridepublic String toString () {//TODO auto-generated method Stubreturn super.tostring ();}}

Step Three: Configure a simple query parameter

<?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" >< Mapper Namespace= "ORM. Users ">    <select id=" Selectuserbyid "  parametertype=" int "resulttype=" orm. Users ">        select * from Users where id = #{id}    </select></mapper>

Fourth step: Test (Compact version)

String resstring= "Configuration.xml";  Reader reader = Resources.getresourceasreader (resstring); Sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). build (reader); sqlsession session =  sessionfactory.opensession (); String statement = "ORM. Users.selectuserbyid "; Users users = (ORM. Users) Session.selectone (statement,2); System.out.println (Users.getusername ());

Fourth Step: Test (complete version)

public class Test {    private static sqlsessionfactory sqlsessionfactory;    private static reader reader;     static{        try{            Reader    = Resources.getresourceasreader ("Configuration.xml");            Sqlsessionfactory = new Sqlsessionfactorybuilder (). build (reader);        catch (Exception e) {            e.printstacktrace ();        }    }    public static Sqlsessionfactory getsession () {        return sqlsessionfactory;    }        public static void Main (string[] args) {        sqlsession session = Sqlsessionfactory.opensession ();        Try {        Users users = (users) session.selectone ("ORM. Users.selectuserbyid ", 2);        System.out.println (users);        } finally {        session.close ();}}    }

  

MyBatis Study Notes First lecture

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.