Beginner mybatis-spring, send a post to record their learning process, first write their own programming steps.
1. Design the database first.
2. According to the design of the data, write the corresponding Pojo, and write the setter and getter method.
3. Write the Mapper interface and write out its corresponding XML file.
4. The preparation of the Mybatisconfig.xml file now appears to require only the writing of its setting.
5. Start writing the Springbeans.xml file.
(1)
Remember to write the DataSource Bean first the class selection for this bean is: org.springframework.jdbc.datasource.SimpleDriverDataSource
The DataSource Bean's four parameters driverclass, URL, username, password can be assigned directly, or can be referenced from the properties file.
Then first create a datasource.properties file, as follows:
The properties file is then referenced in the Springbeans.xml file and the reference code is as follows:
<context:property-placeholder ignore-resource-not-found= "false" location= "classpath:dataSource.properties"/ >
Of course, when using the context to remember to add its namespace, otherwise will be error, the following bold three lines of code:
<BeansXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://www.springframework.org/schema/beans"xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-4.0.xsd">
In this way, you can call the data in the Datasource.properties file.
as follows, assign values to the parameters in the DataSource bean using the calling method:
<BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.SimpleDriverDataSource"> < Propertyname= "Driverclass"value= "${jdbc.database.driver}" /> < Propertyname= "url"value= "${jdbc.database.url}" /> < Propertyname= "username"value= "${jdbc.database.username}" /> < Propertyname= "Password"value= "${jdbc.database.password}" /> </Bean>
(2)
The next step is to configure the Sqlsessionfactory Bean, and we all understand that sqlsessionfactory is the basis for sqlsession, In MyBatis, Sqlsessionfactorybuilder is used to create sqlsessionfactory, and in Mybatis+spring, Sqlsessionfactorybean is provided to support sqlsessionfactory configuration. The configuration method is as follows:
<BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean"> < Propertyname= "DataSource"ref= "DataSource"/> < Propertyname= "Configlocation"value= "Classpath:MyBatisConfig.xml"/> </Bean>
Where DataSource is the DataSource bean,configlocation that we created earlier is the location of the MyBatis configuration file.
(3)
Next, configure the Mapperfactorybean as follows:
<BeanID= "Usermapper"class= "Org.mybatis.spring.mapper.MapperFactoryBean"> < Propertyname= "Mapperinterface"value= "Mapper." Usermapper "/> < Propertyname= "Sqlsessionfactory"ref= "Sqlsessionfactory"/> </Bean>
Where the value of the parameter mapperinterface is the path to the Mapper interface, Sqlsessionfactory is the Sqlsessionfactory Bean configured in the previous step.
Of course, this is the introduction of a mapper formulation method, when the need to configure a lot of mapper, one of the configuration will take a lot of time, which can be configured with Mapperscannerconfigurer, but there is no way to learn the configuration, this content is not the first. It should be simple, not difficult.
6. Next, you can write a main to test, as follows:
Packagedoing;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportMapper. Createtablemapper;ImportMapper. Filenamemapper;ImportMapper. Usermapper;ImportPojo. FileName;ImportPojo. User; Public classMain { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Springbeans.xml"); Usermapper Usermapper= Ctx.getbean (usermapper.class); User User=NewUser (); /*user = Usermapper.getuserbyid (3L); System.out.println (User.getfiles (). Get (1). Getfile_name ()); Filenamemapper filenamemapper = Ctx.getbean (Filenamemapper.class); Filenamemapper.deletefilenamebyuser_name (User.getuser_name ()); Usermapper.deleteuserbyid (3L);*/User.setuser_name ("Zhouchunjie"); User.setpassword ("12345"); User.setmobile ("17721260015"); User.setnote ("No Note"); Usermapper.insertuser (user); }}
So the Mybatis+spring programming step is probably the case, for me this rookie, have a own programming step can let me knock the code when thinking less chaotic.
When programming mybatis+spring, be sure to import mybatis-spring-1.3.0.jar!!!
In this programming process, also found that mybatis can not only the existing tables to query, delete, update and so on, and so on, but also can create a new form! The specific method is too lazy to code word ...
Mybatis-spring Development Record Sticker (novice)