MyBatis ways to develop DAO
1.1 sqlsession Use Range 1.1.1 Sqlsessionfactorybuilder
1 // Reads the total configuration file as a stream 2 Reader reader = Resources.getresourceasreader (" Mybatis-config.xml " 3 4 // generate sqlsessionfactory 5 Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). build (reader); 6 7 // get sqlsession from sqlsessionfactory 8 sqlsession session = Sqlsessionfactory.opensession ();
Create a session factory Sqlsessionfactory with Sqlsessionfactorybuilder
Use Sqlsessionfactorybuilder as a tool class , without the need to use a singleton management sqlsessionfactorybuilder.
When you need to create sqlsessionfactory, you only need to sqlsessionfactorybuilder new once.
1.1.2 Sqlsessionfactory
Create sqlsession with sqlsessionfactory, use singleton mode to manage Sqlsessionfactory (once the factory is created, use one instance).
Once MyBatis and spring are integrated, use Singleton mode to manage Sqlsessionfactory.
1.1.3 Sqlsession
Sqlsession is a user-oriented (programmer) interface.
There are many ways to manipulate databases in sqlsession: SelectOne (returning a single object), SelectList (returning single or multiple objects),.
Sqlsession is thread insecure, and in the Sqlsesion implementation class there are data field properties in addition to the methods in the interface (the method that operates the database).
sqlsession the best application in the method body is defined as local variable use.
1.2 Original DAO development method (Programmer needs to write DAO interface and DAO Implementation Class) 1.2.1 idea
Programmers need to write DAO interfaces and DAO implementation classes.
You need to inject sqlsessionfactory into the DAO implementation class and create it through sqlsessionfactory in the body of the method sqlsession
1.2.2 DAO Interface
1 Public InterfaceUserdao {2 3 //query user information by ID4 PublicUser Finduserbyid (intIdthrowsException;5 6 //Query the list of users based on user names7 PublicList<user> finduserbyname (String name)throwsException;8 9 //Add user InformationTen Public voidInsertuser (user user)throwsException; One A //Delete User Information - Public voidDeleteUser (intIdthrowsException; - the}
1.2.3 DAO Interface Implementation class
1 Public classUserdaoimplImplementsUserdao {2 3 //need to inject sqlsessionfactory into the DAO implementation class4 5 //this is injected through the construction method6 Privatesqlsessionfactory sqlsessionfactory;7 8 PublicUserdaoimpl (sqlsessionfactory sqlsessionfactory) {9 This. Sqlsessionfactory =sqlsessionfactory;Ten One } A - @Override - PublicUser Finduserbyid (intIdthrowsException { theSqlsession sqlsession =sqlsessionfactory.opensession (); - -User user = Sqlsession.selectone ("Test.finduserbyid", id); - + //Freeing Resources - sqlsession.close (); + A returnuser; at - } - - @Override - Public voidInsertuser (user user)throwsException { - inSqlsession sqlsession =sqlsessionfactory.opensession (); - to //Perform an insert operation +Sqlsession.insert ("Test.insertuser", user); - the //Commit a transaction * sqlsession.commit (); $ Panax Notoginseng //Freeing Resources - sqlsession.close (); the + } A the @Override + Public voidDeleteUser (intIdthrowsException { -Sqlsession sqlsession =sqlsessionfactory.opensession (); $ $ //Perform an insert operation -Sqlsession.delete ("Test.deleteuser", id); - the //Commit a transaction - sqlsession.commit ();Wuyi the - //Freeing Resources Wu sqlsession.close (); - About } $ -}
1.2.4 Test Code:
1 Public classUserdaoimpltest {2 3 Privatesqlsessionfactory sqlsessionfactory;4 5 //This method is performed prior to executing Testfinduserbyid6 @Before7 Public voidSetUp ()throwsException {8 //Create Sqlsessionfactory9 Ten //mybatis configuration file OneString resource = "Sqlmapconfig.xml"; A //get configuration file stream -InputStream InputStream =Resources.getresourceasstream (Resource); - the //creating a session factory, passing in MyBatis profile information -Sqlsessionfactory =NewSqlsessionfactorybuilder () - . Build (InputStream); - } + - @Test + Public voidTestfinduserbyid ()throwsException { A //Create an Userdao object atUserdao Userdao =NewUserdaoimpl (sqlsessionfactory); - - //methods for calling Userdao -User user = Userdao.finduserbyid (1); - - System.out.println (user); in } - to}1.2.5 Summary of original DAO development problems
1, the DAO interface implementation class method has a large number of template methods, it is envisaged that the code can be extracted, greatly reducing the workload of the programmer.
2. The ID of statement is hard-coded when calling the Sqlsession method
3, when calling the Sqlsession method, the variable passed in, because the Sqlsession method uses generics, even if the variable type passed in the error, in the compilation phase does not error, not conducive to programmer development.
1.3 Mapper Proxy Method (programmer only needs Mapper interface (equivalent to DAO interface)) 1.3.1 Ideas (Mapper Agent development specification)
Programmers also need to write mapper.xml mapping files
Programmers writing mapper interfaces need to follow some development specifications, MyBatis can automatically generate Mapper interface implementation class proxy objects.
Development specification:
1. Namespace equals Mapper interface address in Mapper.xml
2. The method name in the Mapper.java interface is consistent with the statement ID in the Mapper.xml
3. The method input parameter type in the Mapper.java interface is consistent with the type of parametertype specified in the Mapper.xml statement.
4. The method return value type in the Mapper.java interface is the same as the resulttype specified in mapper.xml statement.
Summarize:
The above development specifications are mainly for the following code unified generation:
User user = Sqlsession.selectone ("Test.finduserbyid", id);
Sqlsession.insert ("Test.insertuser", user);
。。。。
1.3.2 Usermapper.java
1.3.3 Usermapper.xml
1.3.4 Loading Mapper.xml in Sqlmapconfig.xml
1.3.5 Test
1 Privatesqlsessionfactory sqlsessionfactory;2 3 //This method is performed prior to executing Testfinduserbyid4 @Before5 Public voidSetUp ()throwsException {6 //Create Sqlsessionfactory7 8 //mybatis configuration file9String resource = "Sqlmapconfig.xml";Ten //get configuration file stream OneInputStream InputStream =Resources.getresourceasstream (Resource); A - //creating a session factory, passing in MyBatis profile information -Sqlsessionfactory =NewSqlsessionfactorybuilder () the . Build (InputStream); - } - @Test - Public voidTestfinduserbyid ()throwsException { + -Sqlsession sqlsession =sqlsessionfactory.opensession (); + A //Create Usermapper object, MyBatis automatically generate Mapper proxy object atUsermapper usermapper = Sqlsession.getmapper (usermapper.class); - - //methods for calling Usermapper - -User user = Usermapper.finduserbyid (1); - in System.out.println (user); - to + } - //Comprehensive query of user information
1.3.6 Some issues summary 1.3.6.1 proxy object Internal call SelectOne or SelectList
If the Mapper method returns a single Pojo object (not a collection object), the proxy object queries the database internally through SelectOne.
If the Mapper method returns a collection object, the proxy object internally queries the database through SelectList.
1.3.6.2 Mapper Interface method parameters can only have one effect on system development
Mapper interface method parameters can only have one, whether the system is not conducive to extended maintenance.
system frame, DAO The code for the layer is common to the business layer.
even if mapper interface has only one parameter, you can use the Pojo of the wrapper type meet the needs of different business methods.
Note: The parameters of the persistence layer method can be wrapped type, map ... , the package type is not recommended in the service method (it is not conducive to the extensibility of the business layer)
MyBatis ways to develop DAO