MyBatis (1)

Source: Internet
Author: User

Jar package required: mybatis-3.x.x.jar, assuming the need to integrate with spring, also need to increase the relevant package

1: do not care about the red color of the project folder.

2:Follow these steps:

1: Add a jar package

2: create a data source (configuration. xml) 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> <! -- Create an alias for the object class user --> <typeAlias type = "com. forum. po. User" alias = "User"/> </typeAliases> <! -- Configure the data source in 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> <! -- When userMapper. xml is loaded, the "dao" implementation is loaded. --> <mapper resource = "myBatis/userMapper. xml"/> </mappers> </configuration>
3: Create object class USER

Package com. forum. po;/*** User class ** @ author db2admin **/public class User extends Entity {private String name; private 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, which can be called Mapper interface: UserMapper

Package com. forum. dao; import com. forum. po. User; public interface UserMapper {public User findById (String Id );}

5: the implementation of creating dao is different from the implementation of hibernarte here as an xml file, that is, userMapper. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- The namespace equivalent to the dao interface must be the same as the class path of the interface --> <mapper namespace = "com. forum. dao. UserMapper"> <! -- The findById must be the same as the method name in the interface and return a User, which is the alias just now. If the alias is not used, it is difficult to write the class path together. --> <select id = "findById" parameterType = "HashMap" resultType = "User"> select * from butone. student where id =#{ id} </select> </mapper>

6: Benchmark 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 retry *** @ author db2admin **/public class MyBatisTest {/*** get MyBatis SqlSessionFacto Ry * SqlSessionFactory is responsible for creating SqlSession. Once created successfully, you can use the SqlSession instance to run ing statements, 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 (1)

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.