Here is the most basic building: http://www.cnblogs.com/xuyiqing/p/8600888.html
Then do a simple delete and change: http://www.cnblogs.com/xuyiqing/p/8601506.html
But found that the code is too repetitive and so on.
Next, integrate and implement DAO development:
One: Original DAO Development:
Package DAO; Import Pojo. User; Public Interface Userdao { public User Selectuserbyid (Integer ID);}
PackageDAO;Importjava.util.List;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportPojo. User; Public classUserdaoimplImplementsUserdao {//injected Privatesqlsessionfactory sqlsessionfactory; PublicUserdaoimpl (sqlsessionfactory sqlsessionfactory) { This. Sqlsessionfactory =sqlsessionfactory; } //querying a user with a user ID PublicUser Selectuserbyid (Integer id) {sqlsession sqlsession=sqlsessionfactory.opensession (); returnSqlsession.selectone ("Test.finduserbyid", id); } //fuzzy query by user name PublicList<user>selectuserbyusername (Integer id) {sqlsession sqlsession=sqlsessionfactory.opensession (); returnSqlsession.selectlist ("Test.finduserbyid", id); }}
Test class:
PackageJUnit;ImportJava.io.InputStream;Importorg.apache.ibatis.io.Resources;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;ImportOrg.junit.Before;Importorg.junit.Test;ImportDAO. Userdao;ImportDAO. Userdaoimpl;ImportPojo. User; Public classDaotest { Publicsqlsessionfactory sqlsessionfactory; @Before Public voidBefore ()throwsException {String resource= "Sqlmapconfig.xml"; InputStream in=Resources.getresourceasstream (Resource); Sqlsessionfactory=NewSqlsessionfactorybuilder (). build (in); } @Test Public voidTestdao ()throwsException {Userdao Userdao=NewUserdaoimpl (sqlsessionfactory); User User= Userdao.selectuserbyid (10); SYSTEM.OUT.PRINTLN (user); }}
However, it is still found that code duplication, wasting resources and other issues:
So think of mapper dynamic agent development:
Packagemapper;ImportPojo. User; Public InterfaceUsermapper {//follow four principles//interface Method name = = ID name in User.xml//The return value type is identical to the return value type in the Mapper.xml file//the parameter type of the method is the same as the type of the entry in the Mapper.xml//namespace binds this interface//if an object is returned, call the SelectOne method//If it is list, call the SelectList method PublicUser Finduserbyid (Integer ID); }
Usermapper.xml
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE mapperpublic "-//mybatis.org//dtd Mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><!--Write SQL statements -<Mappernamespace= "Mapper." Usermapper "> <!--querying a user by ID - <SelectID= "Finduserbyid"ParameterType= "Integer"Resulttype= "Pojo." User ">SELECT * from user where id = #{v}</Select> <!--//Fuzzy query user list based on user name #{} SELECT * FROM user where id =? Placeholder? = = ' Five ' ${} SELECT * from user where username like '% Five ' string concatenation - <SelectID= "Finduserbyusername"ParameterType= "String"Resulttype= "Pojo." User ">SELECT * from user where username like "%" #{haha} "%"</Select> <!--Add User - <InsertID= "Insertuser"ParameterType= "Pojo." User "> <SelectkeyKeyproperty= "id"Resulttype= "Integer"Order= "after">Select last_insert_id ()</Selectkey>INSERT INTO User (Username,birthday,address,sex) VALUES (#{username},#{birthday},#{address},#{sex}) </Insert> <!--Update - <UpdateID= "Updateuserbyid"ParameterType= "Pojo." User ">Update user Set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address} WHERE id = #{id}</Update> <!--Delete - <DeleteID= "Deleteuserbyid"ParameterType= "Integer">Delete from user where id = #{vvvvv}</Delete></Mapper>
Master configuration file Sqlmapconfig.xml:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><Configuration> <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <TransactionManagertype= "JDBC" /> <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver" /> < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/mybatis?characterencoding=utf-8" /> < Propertyname= "username"value= "root" /> < Propertyname= "Password"value= "Xuyiqing" /> </DataSource> </Environment> </Environments> <mappers> <MapperResource= "Mapper/usermapper.xml"/> </mappers></Configuration>
Test class:
PackageJUnit;ImportJava.io.InputStream;Importorg.apache.ibatis.io.Resources;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;Importorg.junit.Test;ImportMapper. Usermapper;ImportPojo. User; Public classmybatismappertest {@Test Public voidTestmapper ()throwsException {String resource= "Sqlmapconfig.xml"; InputStream in=Resources.getresourceasstream (Resource); Sqlsessionfactory sqlsessionfactory=NewSqlsessionfactorybuilder (). build (in); Sqlsession sqlsession=sqlsessionfactory.opensession (); //sqlsession automatically generates an implementation class for the interfaceUsermapper usermapper = Sqlsession.getmapper (usermapper.class); User User= Usermapper.finduserbyid (10); SYSTEM.OUT.PRINTLN (user); }}
In general, it is recommended to use Mapper dynamic agent development
MyBatis Framework III: DAO layer Development, Mapper dynamic agent development