SpringMvc (3) Configure Mybatis

Source: Internet
Author: User

The SpringMvc framework has been completed before. It is not very convenient to directly connect to the database using jdbc. Here Mybatis is set up. I have never used hibernate, and Mybatis does not need to write SQL by myself. xml, which can be automatically generated using a plug-in, as follows:

First, install Mybatis Generator in Eclipse and click help -- install new software. Enter the following address:

Http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/


After the installation is complete, you may need to restart Eclipse. Then we can find a place at will, right-click the test package, and choose create, Other, find as shown in Figure


Select this Generator and click "Next" to determine the name location. A generatorConfig. xml file is automatically generated. After the file is opened, it is just a framework. I have filled in the file and explained it as follows:

 
  
    
    
    
       
       
       
       
       
       
       
       
   
   
   
 
We can see that this file can be connected to the database and automatically reverse engineer the generation of entity classes, managers, and sqlmap Based on the table structure. This file is very simple. You just need to determine the location. here you must pay attention to the entity location, because SQL. the above path will be used in the xml file. If you do not have a correct location and change the location of the entity after generation, then the SQL statement is used. you can change the xml file to death.

After confirming that the information is correct (in this case, we can delete the entity and manager we wrote when spring was configured), right-click generatorConfig. xml, and click the following


It is automatically generated,


We can see that entity, manager, and sqlMap are automatically generated, and sqlMap can be directly cut off and put under the resource package. The service reports an error because we deleted the previous dao. For generatorConfig. xml can be deleted directly. If you want to keep it, the database will change. In fact, you can create another project to generatorConfig. xml is placed in another project, and can be generated to this project as long as the path is configured in another project. Staying in this project is still in the way. Then let's take a closer look at these automatically generated items. Entity classes are generated based on database fields and data types. Then each corresponding example is used to encapsulate the query conditions. Check the code to understand that the manager provides a basic interface for addition, deletion, modification, and query. The interface name directly corresponds to the SQL. xml file under sqlMap. All query for a single table. What should I do with the Combined Query? I just need to add it in mapper. It is very effort-saving to automatically generate these items.

Then the service still reports an error. After deleting the original items, how can we provide the new services.

You only need to continue with the following configuration in application. xml.

 
 
  
  
 
 
     
  
  
 
     
  
  
 
Then service Miles can be used, but we remember that the available Query type is a joint query of its own. If the automatically generated SQL statement does not exist, we can customize it. Mapper Addition Method

/*** Custom query available type */List
 
  
SelectLastTypes ();
 

At the same time, the corresponding SQL statement is added with selectLastTypes as the id

 
   Selecttype_idastype_id, type_nameastype_name fromaccount_type wheretype_idnotin (selectdistinct type_parent from account_type)

Service:

Package com. springMvc. service. impl; import java. util. list; import org. apache. log4j. logger; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. service; import com. springMvc. dao. manager. accountTypeMapper; import com. springMvc. model. typeInfo; import com. springMvc. service. typeService; @ Servicepublic class TypeServiceImpl implements TypeService {// loggerprivate final static Logger logger = Logger. getLogger (TypeServiceImpl. class); // @ Autowired // automatically assemble dao bean // private TypeManager typeManager; @ Autowiredprivate AccountTypeMapper accountTypeMapper; @ Overridepublic List
 
  
SelectAllTypes () {logger.info ("---------------- retrieve available type, Service layer --! "); Return accountTypeMapper. selectLastTypes ();}}
 
Package com. springMvc. service. impl; import java. SQL. date; import org. apache. log4j. logger; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. service; import com. springMvc. dao. entity. accountInfo; import com. springMvc. dao. manager. accountInfoMapper; import com. springMvc. service. accountService; @ Service // A servicepublic class AccountServiceImpl implemen marked as a Service layer Ts AccountService {// loggerprivate final static Logger logger = Logger. getLogger (AccountServiceImpl. class); // @ Autowired // automatically assemble dao bean // private AccountManager accountManager; @ Autowiredprivate AccountInfoMapper accountInfoMapper; @ Overridepublic void insertAccout (Date acc_time, Integer acc_jine, Integer accept, integer acc_type, String acc_beizhu) {logger.info ("---------------- Insert the account to the Service layer --! "); // Account ac = new Account (); AccountInfo ac = new AccountInfo (); ac. setAccTime (acc_time); ac. setAccJine (acc_jine); ac. setAccShouzhi (acc_shouzhi = 1? True: false); ac. setAccType (acc_type); ac. setAccBeizhu (acc_beizhu); // accountManager. insertAccount (ac); accountInfoMapper. insert (ac );}}
If the controller does not need to be moved, it will be OK. Then run tomcat again to see if it is useful. Here, example has never been used. Here is a simple example. For example, if I want to query the expenditure accounts of a certain day, the condition is dateX, and the income and expenditure type is false, it is as follows:

AccountInfoExample ex = new AccountInfoExample (); // encapsulation condition ex. createCriteria (). andacctimestamps to (dateX). andaccshouzhito to (false); List list = accountInfoMapper. selectByExample (ex );
The returned list is the expected result. Do you understand it? There are many other things about spring and mybatis. Here we will give you a simple example. Also share some jar packages of common java tools

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.