Mybitis (Ibitis) series of essays: mybitis instance of getting Started

Source: Internet
Author: User

Mybits formerly known as Ibitis, it is a semi-automated ORM framework relative to hibernate. In the spirit of sharing and learning purposes, the author will continue to study notes and code, I hope to learn mybitis students have some help.
Since my test database is MySQL, my colleagues are now loading MySQL, then preparing MySQL driver and downloading Mybitis (http://code.google.com/p/mybatis/) from the official website. I downloaded the latest mybatis-3.2.2.zip.

Create a database test

Then create the table T_user

[SQL]View PlainCopy
  1. DROP TABLE IF EXISTS ' T_user ';
  2. CREATE TABLE ' T_user ' (
  3. ' ID ' int (one) not NULL auto_increment,
  4. 'name ' varchar ( DEFAULT NULL) ,
  5. ' Pass ' varchar DEFAULT NULL,
  6. PRIMARY KEY (' id ')
  7. ) Engine=innodb auto_increment=24 DEFAULT CHARSET=GBK;
  8. INSERT into ' T_user ' VALUES (' 1 ', ' ssssssssssssssss ', ' DDSSSSSSSDD ');


Then create the Java Bean object corresponding to the table

[Java]View PlainCopy
  1. <span style="color: #000000;"  > Packagecom.jefry;
  2. Public class User {
  3. private int id;
  4. private String name;
  5. private String Pass;
  6. Public String Getpass () {
  7. return pass;
  8. }
  9. public void SetPass (String pass) {
  10. This.pass = pass;
  11. }
  12. public int getId () {
  13. return ID;
  14. }
  15. public void setId (int id) {
  16. this.id = ID;
  17. }
  18. Public String GetName () {
  19. return name;
  20. }
  21. public void SetName (String name) {
  22. this.name = name;
  23. }
  24. }</span>


Next, create and configure the Mybatis-config.xml file under the Classpath directory

The contents are as follows:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE Configuration
  3. Public "-//mybatis.org//dtd Config 3.0//en"
  4. "Http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <environments default="Development">
  7. <environment id="Development">
  8. <transactionmanager type="JDBC"/>
  9. <dataSource type="pooled">
  10. <property name= "driver" value="com.mysql.jdbc.Driver"/>
  11. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
  12. <property name= "username" value="root"/>
  13. <property name= "password" value="root"/>
  14. </dataSource>
  15. </Environment>
  16. </environments>
  17. <mappers>
  18. <!--This file will be created --
  19. <mapper resource="Com/jefry/usermapper.xml"/>
  20. </mappers>
  21. </configuration>


With <mapper resource= "Com/jefry/usermapper.xml"/> Configuration items in Mybatis-config.xml, we create a file under package Com.jefry UserMapper.xml
The contents are as follows:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE Mapper
  3. Public "-//mybatis.org//dtd Mapper 3.0//en"
  4. "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="User">
  6. <!--Resulttype represents an entity object such as Com.jefry.User -
  7. <Select id= "selectuser" parametertype="int" resulttype="Com.jefry.User" >
  8. SELECT * from t_user where id = #{id}
  9. </Select>
  10. </mapper>


Writing test Code

[Java]View PlainCopy
  1. Package Com.jefry;
  2. Import java.io.IOException;
  3. Import Java.io.InputStream;
  4. Import org.apache.ibatis.io.Resources;
  5. Import org.apache.ibatis.session.SqlSession;
  6. Import Org.apache.ibatis.session.SqlSessionFactory;
  7. Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. Public class Test {
  9. static String resource = "Mybatis-config.xml";
  10. public static void Main (string[] args) throws IOException {
  11. //TODO auto-generated method stub
  12. InputStream InputStream = resources.getresourceasstream (Resource);
  13. Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
  14. sqlsession session = Sqlsessionfactory.opensession ();
  15. try {
  16. //user.selectuser in user usermapper.xml file <mapper namespace= "user" > Namespace value
  17. //selectuser usermapper.xml file in the label <select id= "Selectuser" parametertype= "int" resulttype= "Com.jefry.User" ID value in >
  18. User user = Session.selectone ("User.selectuser", 1);
  19. System.out.println ("user.getname () =" + User.getname ());
  20. } finally {
  21. Session.close ();
  22. }
  23. }
  24. }


By running it can result: User.getname () =ssssssssssssssss

Mybitis (Ibitis) series of essays: mybitis instance of 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.