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
- DROP TABLE IF EXISTS ' T_user ';
- CREATE TABLE ' T_user ' (
- ' ID ' int (one) not NULL auto_increment,
- 'name ' varchar ( DEFAULT NULL) ,
- ' Pass ' varchar DEFAULT NULL,
- PRIMARY KEY (' id ')
- ) Engine=innodb auto_increment=24 DEFAULT CHARSET=GBK;
- INSERT into ' T_user ' VALUES (' 1 ', ' ssssssssssssssss ', ' DDSSSSSSSDD ');
Then create the Java Bean object corresponding to the table
[Java]View PlainCopy
- <span style="color: #000000;" > Packagecom.jefry;
- Public class User {
- private int id;
- private String name;
- private String Pass;
- Public String Getpass () {
- return pass;
- }
- public void SetPass (String pass) {
- This.pass = pass;
- }
- public int getId () {
- return ID;
- }
- public void setId (int id) {
- this.id = ID;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }</span>
Next, create and configure the Mybatis-config.xml file under the Classpath directory
The contents are as follows:
[HTML]View PlainCopy
- <? 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>
- <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://127.0.0.1:3306/test"/>
- <property name= "username" value="root"/>
- <property name= "password" value="root"/>
- </dataSource>
- </Environment>
- </environments>
- <mappers>
- <!--This file will be created --
- <mapper resource="Com/jefry/usermapper.xml"/>
- </mappers>
- </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
- <? 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="User">
- <!--Resulttype represents an entity object such as Com.jefry.User -
- <Select id= "selectuser" parametertype="int" resulttype="Com.jefry.User" >
- SELECT * from t_user where id = #{id}
- </Select>
- </mapper>
Writing test Code
[Java]View PlainCopy
- Package Com.jefry;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import org.apache.ibatis.io.Resources;
- Import org.apache.ibatis.session.SqlSession;
- Import Org.apache.ibatis.session.SqlSessionFactory;
- Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;
- Public class Test {
- static String resource = "Mybatis-config.xml";
- public static void Main (string[] args) throws IOException {
- //TODO auto-generated method stub
- InputStream InputStream = resources.getresourceasstream (Resource);
- Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
- sqlsession session = Sqlsessionfactory.opensession ();
- try {
- //user.selectuser in user usermapper.xml file <mapper namespace= "user" > Namespace value
- //selectuser usermapper.xml file in the label <select id= "Selectuser" parametertype= "int" resulttype= "Com.jefry.User" ID value in >
- User user = Session.selectone ("User.selectuser", 1);
- System.out.println ("user.getname () =" + User.getname ());
- } finally {
- Session.close ();
- }
- }
- }
By running it can result: User.getname () =ssssssssssssssss
Mybitis (Ibitis) series of essays: mybitis instance of getting Started