First, the original DAO development Way
Userdao.java
public interface Userdao{public User Finduserbyid (Serializable id);p ublic list<user> findusersbyname (String name );}
Userdaoimpl.java
The public class Userdaoimpl implements Userdao{//sqlsessionfactory is a singleton that should not be created in a specific instance and requires an external dependency injected into the private sqlsessionfactory Sqlsessionfactory;public Userdaoimpl (sqlsessionfactory sqlsessionfactory) {this.sqlsessionfactory = Sqlsessionfactory;} @Overridepublic User Finduserbyid (Serializable id) {sqlsession sqlsession = sqlsessionfactory.opensession (); User user = Sqlsession.selectone ("Test.finduserbyid", id); Sqlsession.close (); return user;} @Overridepublic list<user> findusersbyname (String name) {sqlsession sqlsession = sqlsessionfactory.opensession ( ); list<user> list = sqlsession.selectlist ("Test.finduserbyname", name); Sqlsession.close (); return list;}}
Test.java
public class Userdaotest{private Sqlsessionfactory sqlsessionfactory;private Userdao userdao;{ String resource = "Sqlmapconfig.xml"; InputStream is;try{is = Resources.getresourceasstream (Resource); Sqlsessionfactorybuilder builder = new Sqlsessionfactorybuilder (); sqlsessionfactory = Builder.build (is); UserDao = new Userdaoimpl (sqlsessionfactory);} catch (IOException e) {e.printstacktrace ();}} @Testpublic void Testfinduserbyid () {User user = Userdao.finduserbyid (27); SYSTEM.OUT.PRINTLN (user);} @Testpublic void Testfindusersbyname () {list<user> List = Userdao.findusersbyname ("Xiaoming"); for (User user:list) { SYSTEM.OUT.PRINTLN (user);}}}
There are some problems with the original DAO Development:
(1) There is a certain amount of template code
For example: Create sqlsession through Sqlsessionfactory, invoke Sqlsession method to manipulate database, close sqlsession.
(2) There are some hard-coded
When you invoke the Sqlsession method to manipulate the database, you need to specify the ID of the statement, which is hard-coded.
Second, mapper agent development mode
Mapper Agent development method, programmers only need to write the Mapper interface (equivalent to DAO interface) can be. MyBatis automatically generates a dynamic proxy implementation class for the Mapper interface. However, to implement the Mapper agent development method, we need to follow some development specifications.
(1) The fully qualified name of the Mapper interface is the same as the value of namespace for the mapper mapping file .
(2) The method name of the Mapper interface is the same as the ID of the statement in the mapper mapping file .
(3) The method parameter of the Mapper interface can only have one, and the type is consistent with the value of ParameterType statement in the mapper mapping file.
(4) The return value type of the Mapper interface should be consistent with the Resulttype value in the Mapper mapping file or the type value in RESULTMAP statement.
Mapper.java
Public interface Usermapper{public user Finduserbyid (int id);p ublic void saveuser (user user);
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 "><!--namespace: At this time with the Mapper proxy mode, its value must be equal to the fully qualified name of the corresponding mapper interface -<Mappernamespace= "Com.kiwi.mapper.UserMapper"> <!--query a user by ID - <SelectID= "Finduserbyid"ParameterType= "int"Resulttype= "Com.kiwi.domain.User">SELECT * from USER WHERE id = #{id}</Select> <!--fuzzy query by name - <SelectID= "Finduserbyname"ParameterType= "Java.lang.String"Resulttype= "Com.kiwi.domain.User">SELECT * from USER WHERE username like '%${value}% '</Select> <!--add a user and return the inserted ID - <InsertID= "Saveuser"ParameterType= "Com.kiwi.domain.User"> <SelectkeyKeyproperty= "id"Resulttype= "int"Order= "after">SELECT last_insert_id ()</Selectkey>INSERT into USER (username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address})</Insert> <!--Delete a user - <DeleteID= "DeleteUser"ParameterType= "int">DELETE from USER WHERE id = #{id}</Delete> <!--Modify a user - <UpdateID= "UpdateUser"ParameterType= "Com.kiwi.domain.User">UPDATE USER SET username = #{username},address = #{address} WHERE id = #{id}</Update></Mapper>
Sqlmapconfig.xml
<?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> <!--Configure the MyBatis environment information, which, when integrated with spring, will be managed by spring - <Environmentsdefault= "Development"> <EnvironmentID= "Development"> <!--Configuring JDBC Transaction control, managed by MyBatis - <TransactionManagertype= "JDBC"></TransactionManager> <!--Configure the data source with DBCP connection pool - <DataSourcetype= "Pooled"> < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver" /> < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/mybatis?useunicode=true&characterencoding=utf8" /> < Propertyname= "username"value= "root" /> < Propertyname= "Password"value= "123456" /> </DataSource> </Environment> </Environments> <!--Load Mapper - <mappers> <MapperResource= "Mapper/usermapper.xml"/> </mappers></Configuration>
Test.java
Private sqlsessionfactory sqlsessionfactory; @Beforepublic void SetUp () throws Exception{inputstream InputStream = Resources.getresourceasstream ("Sqlmapconfig.xml"); sqlsessionfactory = new Sqlsessionfactorybuilder (). Build ( InputStream);} @Testpublic void Testfinduserbyid () {sqlsession sqlsession = sqlsessionfactory.opensession (); Usermapper mapper = Sqlsession.getmapper (Usermapper.class); User user = Mapper.finduserbyid (26); SYSTEM.OUT.PRINTLN (user); Sqlsession.close ();} @Testpublic void Testsaveuser () {sqlsession sqlsession = sqlsessionfactory.opensession (); Usermapper mapper = Sqlsession.getmapper (Usermapper.class); User user = New User ("WWW", "1", New Date (), "Haidian District, Beijing"); Mapper.saveuser (user); SYSTEM.OUT.PRINTLN (user); Sqlsession.commit (); Sqlsession.close ();}}
Third, global configuration 1.properties
2.typeAliases
Category name, only the type alias definition of the PO class.
Alias |
Types of mappings |
_byte |
Byte |
_long |
Long |
_short |
Short |
_int |
Int |
_integer |
Int |
_double |
Double |
_float |
Float |
_boolean |
Boolean |
String |
String |
Byte |
Byte |
Long |
Long |
Short |
Short |
Int |
Integer |
Integer |
Integer |
Double |
Double |
Float |
Float |
Boolean |
Boolean |
Date |
Date |
Decimal |
BigDecimal |
BigDecimal |
BigDecimal |
MyBatis Basic Learning (II.)-Developing DAO Way