MyBatis Real-World tutorials (MyBatis in action): Development Environment Building

Source: Internet
Author: User

MyBatis development environment to build, select: Eclipse java EE version, MySQL 5.1, JDK 1.7,mybatis3.2.0.jar package. These software tools can be downloaded to the respective official website.
First, create a dynamic Web project named Mybaits.
1. At this stage, you can build a Java project directly, but generally you are developing Web projects, and this series of tutorials ends up being web-based, so you start with Web engineering.
2. Copy the Mybatis-3.2.0-snapshot.jar,mysql-connector-java-5.1.22-bin.jar to the Lib directory of the Web project.
3. Create MySQL test database and user table, note that the UTF-8 encoding is used here

Create a user table and insert a test data
Program code
Create TABLE ' user ' (
' id ' int (one) not NULL auto_increment,
' userName ' varchar DEFAULT NULL,
' Userage ' int (one) DEFAULT NULL,
' useraddress ' varchar ($) DEFAULT NULL,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=2 DEFAULT Charset=utf8;


Insert into ' user ' VALUES (' 1 ', ' Summer ', ' + ', ' shanghai,pudong ');

So far, the preparatory work has been completed. The following is the real configuration of the MyBatis project.
1. Create two source directories in MyBatis, respectively, SRC_USER,TEST_SRC, set up in the following way, right click on the Javaresource mouse button.


2. Set the MyBatis configuration file: Configuration.xml, create this file in the Src_user directory, as follows:
Program code
<?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= "User" type= "Com.yihaomen.mybatis.model.User"/>
</typeAliases>

<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/mybatis"/>
<property name= "username" value= "root"/>
<property name= "password" value= "password"/>
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource= "Com/yihaomen/mybatis/model/user.xml"/>
</mappers>
</configuration>


3. Create the Java class corresponding to the database and the mapping file.
build the Package:com.yihaomen.mybatis.model under Src_user and create the User class under this package:
Program code
Package Com.yihaomen.mybatis.model;

public class User {

private int id;
Private String UserName;
Private String userage;
Private String useraddress;

public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetUserName () {
return userName;
}
public void Setusername (String userName) {
This.username = UserName;
}
Public String Getuserage () {
return userage;
}
public void Setuserage (String userage) {
This.userage = Userage;
}
Public String getuseraddress () {
return useraddress;
}
public void setuseraddress (String useraddress) {
this.useraddress = useraddress;
}

}


also establish this User mapping file User.xml:
Program code
<?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= "Com.yihaomen.mybatis.models.UserMapper" >
<select id= "Selectuserbyid" parametertype= "int" resulttype= "User" >
SELECT * from ' user ' where id = #{id}
</select>
</mapper>


under the face of these several configuration files are explained below:
1.configuration.xml is mybatis used to build sessionfactory, which mainly contains the database connection related things, as well as the Java class corresponding aliases, such as <typealias alias= "User" Type= "Com.yihaomen.mybatis.model.User"/> This alias is very important, you in the specific class mapping, such as User.xml Resulttype is corresponding here. To be consistent, of course, there is a separate definition of the resulttype here, and then the other way around.
2. Configuration.xml inside the <mapper resource= "Com/yihaomen/mybatis/model/user.xml"/> is the XML configuration file that contains the class to be mapped.
3. In the User.xml file, the main definition is the various SQL statements, as well as the parameters of these statements, as well as the types to be returned.

Start Testing
build Com.yihaomen.test The package under the TEST_SRC source directory and set up test class tests:
Program code
Package com.yihaomen.test;

Import Java.io.Reader;

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.yihaomen.mybatis.model.User;

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 {
User user = (user) Session.selectone ("Com.yihaomen.mybatis.models.UserMapper.selectUserByID", 1);
System.out.println (User.getuseraddress ());
System.out.println (User.getusername ());
} finally {
Session.close ();
}
}
}

now run this program, is not to get the results of the query. Congratulations, the environment is set up successfully, the second chapter, will be about the interface-based operation, add and delete changes.
the entire project directory structure is as follows:

Source: http://www.yihaomen.com/article/java/303.htm

From for notes (Wiz)

MyBatis Real-World tutorials (MyBatis in action): Development Environment Building

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.