The engineering configuration for spring's integrated MyBatis has been fully detailed in the Spring+mybatis practice-engineering configuration. Here, record several ways to access the database through MyBatis.
- Access to the database via Sqlsessiontemplate.
- Access to the database through the DAO interface.
- Accessing the database using the @select annotation form
1. Use MyBatis in spring Framework for data operation configuration, refer to Spring+mybatis Practice-spring-datasources.xml file for engineering configuration.
2, demo1-through the sqlsessiontemplate for database access:
First, the entity class user is defined, and the corresponding data tables are established in the database (MySQL).
1 Public class User {2 private int userId;3 private String name;4 private String pwd;5 private String email;6 private String address;7 private String signature;8 private String phone;9 Ten / * constructor and getter, setter method * / One}
Next, write the mapping file User.xml for the table Tb_user in the database:
<?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 "><Mappernamespace= "Com.crazysnail.dao.UserDao"> <SelectID= "Getuserbyuserid"Resulttype= "User"ParameterType= "int">SELECT * from Tb_user where Userid=#{userid}</Select></Mapper>
Note: Note the namespace specified at the beginning of the mapping file. 】
At this point, data access can be done through sqlsessiontemplate.
@Servicepublic class UserService { @Autowired private sqlsessiontemplate sqlsessiontemplate; Public user Getuserbyuserid (int userId) { User user = (user) Sqlsessiontemplate.selectone (" Com.crazysnail.dao.UserDao.getUserByUserId ", 1); return user; }}
Note: The com.crazysnail.dao.UserDao.getUserByUserId in the Sqlsessiontemplate SelectOne method corresponds to the ID of the SELECT statement in the mapping file, and the parameter is passed in through the second of the SelectOne 。 】
3, demo2-through the DAO interface for database access.
Access to the database through the above-mentioned way, more cumbersome, not intuitive. At this point, you can invoke the database operation in the map file User.xml in a different way.
Then Demo1 the work done, continue to write the Userdao interface.
1 Package Com.crazysnail.dao; 2 3 import Com.crazysnail.domain.User; 4 5 Public interface Userdao {6public User getuserbyuserid (int userId); 7 8 }
Add the following configuration in the Spring configuration file (This configuration has been added to the Spring+mybatis practice-project configuration's Spring-datasources.xml file):
<!-- used to map interfaces to specific instances so that the relevant DAO interfaces can be injected directly into the service for data access - < class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" P: Sqlsessionfactory-ref= "Sqlsessionfactory" p:basepackage= " Com.crazysnail.dao " />
The bean is used to map the DAO interface to the corresponding mapping file, where the namespace of the mapping file corresponds to the full name of the DAO interface. This causes the Userdao interface to be associated with the User.xml file.
Description: An association relationship is established by specifying the namespace name when the User.xml file is defined. The User.xml namespace is defined as Com.crazysnail.dao.UserDao, which is the full name of the Userdao interface. Also, it is important to note that The method name declared in the Userdao interface corresponds to the ID of the data access procedure defined in User.xml, and the parameter type of the method in the interface corresponds to the parametertype of the data access procedure in the User.xml, which corresponds to the parameters in the SQL statement in the data access procedure in User.xml. The return value type of the interface method corresponds to the type of the Resulttype declaration in User.xml.
such as the method in the Userdao interface,
1 public User getuserbyuserid (int userId);
Corresponds to the user.xml in the
<id= "Getuserbyuserid" resulttype= "User" parametertype = "int"> select * from tb_user where Userid=#{userid}</ Select>
The description ends. 】
Finally, you can perform data processing by injecting Userdao in the service and invoking the interfaces declared in Userdao.
1 @Service 2 Public class UserService {3 @Autowired4 private Userdao Userdao; 5 6 Public User getUser (int id) {7 return Userdao.getuserbyuserid (ID); 8 }9 }
Note : The XML that makes MyBatis data access takes effect when Sqlsessionfactorybean is configured by p:mapperlocations= "classpath:com/crazysnail/ Domain/mybatisconfig/*.xml "to declare.
4. demo3-omitting the mapping file, using annotations:
The annotations on data access provided in MyBatis's org.apache.ibatis.annotations package include @select, @Update, @Delete, @Insert, which can be used to inject simple SQL statements directly, omitting the mapping file. You can also use map files and annotations together.
When using annotations alone , you can omit
<id= "Sqlsessionfactory" class= " Org.mybatis.spring.SqlSessionFactoryBean " p:datasource-ref=" DataSource " P : configlocation= "Classpath:mybatisConfig.xml" p:mapperlocations= " Classpath:com/crazysnail/domain/mybatisconfig/*.xml " />
The p:mapperlocations= "Classpath:com/crazysnail/domain/mybatisconfig/*.xml" in the configuration of the map file location.
instance, overwriting the DAO interface in Demo2, and removing the related configuration of the mapping file, the code in the service layer will not change.
1 Package Com.crazysnail.dao; 2 3 import Com.crazysnail.domain.User; 4 5 Public interface Userdao {6 @Select ("select * from Tb_user where Userid=#{userid}") 7public User getuserbyuserid (int userId); 8 9 }