1. Create a Web project, add a jar package
2. Create the experimental table user_t
3. Create the Conf.xml file under SRC, as follows
<?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> <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <TransactionManagertype= "JDBC" /> <!--Configure database connection information - <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver" /> < Propertyname= "url"value= "Jdbc:mysql://localhost/mi" /> < Propertyname= "username"value= "root" /> < Propertyname= "Password"value= "Dbdaocom" /> </DataSource> </Environment> </Environments> </Configuration>
2. Define the entity class corresponding to the table,
Package com.mi.entity; Public class User { privateint ID; Private String userName; Private String password; Private Integer age; ...}
3. Define the SQL mapping file that operates the users table Usermapper.xml
<?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 "><Mappernamespace= "Com.mi.dao.UserInfoMapper"> <SelectID= "Queryuserinfo"Resulttype= "Com.mi.entity.User">SELECT id,user_name username,age from user_t where 1=1</Select> <SelectID= "GetCount"Resulttype= "int">SELECT Count (*) from user_t</Select> </Mapper>
4. Register the Usermapper.xml file in the Conf.xml file
<?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> <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <TransactionManagertype= "JDBC" /> <!--Configure database connection information - <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver" /> < Propertyname= "url"value= "Jdbc:mysql://localhost/mi" /> < Propertyname= "username"value= "root" /> < Propertyname= "Password"value= "Dbdaocom" /> </DataSource> </Environment> </Environments> <mappers> <MapperResource= "Com/mi/mapping/userinfomapper.xml"/><!--Register the Usermapper.xml file, - </mappers></Configuration>
5. Write the test code: Execute the defined SELECT statement
To create a Test1 class, write the following test code:
PackageCom.mi.demo;ImportJava.io.InputStream;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;ImportCom.mi.entity.User; Public classTest1 { Public Static voidMain (string[] args) {//configuration file for MyBatisString resource = "Conf.xml"; //load the MyBatis configuration file using the ClassLoader (it also loads the associated mapping file)InputStream is = Test1.class. getClassLoader (). getResourceAsStream (Resource); //building a factory in sqlsessionSqlsessionfactory sessionfactory =NewSqlsessionfactorybuilder (). Build (IS); //use the resources class provided by MyBatis to load the MyBatis configuration file (It also loads the associated mapping file)//Reader reader = resources.getresourceasreader (Resource); //building a factory in sqlsession//sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). build (reader); //Create a sqlsession that can execute SQL in the mapping filesqlsession session =sessionfactory.opensession (); /*** Mapped SQL identification string, * Com.mi.mapping.UserInfoMapper is the value of the namespace attribute of the mapper tag in the Userinfomapper.xml file, * Queryuserinfo is the id attribute value of the select tag, and the value of the id attribute of the SELECT tag can be used to find the SQL to execute*/String Statement= "Com.mi.mapping.UserInfoMapper.queryUserInfo";//identity string for mapping SQLUser user = Session.selectone (statement,1);//executes a query that returns the SQL for a unique user objectSystem.out.println (User.getid ()); System.out.println (User.getusername ()); System.out.println (User.getage ()); }}
The results are as follows:
MyBatis (i) installation