spring integrates mytabis
Is to use the function of mybatis in the spring architecture
It is equal to the mybatis tar package imported from the code written in non spring architecture to do the operation of the core configuration file, mapping configuration file and dynamic proxy interface of mybatis.
Then there are operations such as dao, service and controller to transfer data and convert data types.
Non spring environment steps for mybatis
1. Entity class and table
2. Business layer interface and Implementation
3. Data layer interface
4.Mybatis core configuration
5.Mybatis mapping configuration
6. Client program test function
spring environment steps for mybatis
1. Entity class and table
2. Business layer interface and Implementation (provide injection operation of data layer interface)
3. Data layer interface
4.Mybatis core configuration (spring control, this file is omitted)
5.Mybatis mapping configuration
6. Client program test function (using spring to obtain bean s)
7.Spring core configuration file
8. Application of Druid data source (optional)
9.Spring integrates MyBatis
Basic preparation
1. Import Spring coordinates, MyBatis coordinates, MySQL coordinates and Druid coordinates
- Business class and interface preparation
2. Create database tables and create corresponding entity class Account
3. Define business layer interface and data layer interface
4. Call the data layer interface in the business layer and realize the call of business methods
-Basic profile
5.jdbc.properties
6.MyBatis mapping configuration file
-Basic preparation before integration
1. The spring configuration file, plus the context namespace, is used to load the properties file
2. Open and load the properties file
3. Configure data source druid (standby)
4. Define service layer beans and inject dao layer beans
5.dao bean s do not need to be defined and are generated automatically by proxy
Import Spring coordinates, mybatis coordinates, MySQL coordinates
<modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <name>spring_day01_account</name> <groupId>com.itheima</groupId> <artifactId>spring_day01_account</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies>
dao Interface public interface AccountDao { void save(Account account); void delete(Integer id); void update(Account account); List<Account> findAll(); Account findById(Integer id); }
service Interface public interface AccountService { void save(Account account); void delete(Integer id); void update(Account account); List<Account> findAll(); Account findById(Integer id); }
jdbc.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring_db jdbc.username=root jdbc.password=itheima
mybatis core configuration file 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> <properties resource="jdbc.properties"></properties> <typeAliases> <package name="com.itheima.domain"/> </typeAliases> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </dataSource> </environment> </environments> <mappers> <package name="com.itheima.dao"></package> </mappers> </configuration>
spring configuration file ApplicationContext xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
AccountDao. The XML configuration file is the mapping configuration file of mybatis
Used to configure SQL statement operation 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"> <mapper namespace="com.itheima.dao.AccountDao"> <!--Configuration basis id query--> <select id="findById" resultType="account" parameterType="int"> select * from account where id = #{id} </select> <!--Configure query all--> <select id="findAll" resultType="account"> select * from account </select> <!--Configuration save--> <insert id="save" parameterType="account"> insert into account(name,money)values(#{name},#{money}) </insert> <!--Configuration deletion--> <delete id="delete" parameterType="int"> delete from account where id = #{id} </delete> <!--Configure query by name--> <select id="findByName" resultType="account" parameterType="string"> select * from account where name = #{name} </select> <!--Configuration update--> <update id="update" parameterType="account"> update account set name=#{name},money=#{money} where id=#{id} </update> </mapper>
Next, in the pom file
Add the coordinates of spring and druid
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency>
This is the tar package of mybatis integrating spring
With it, spring can know that mybatis has come in and control the function of mybatis
But this is also a tar belonging to mybatis
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency>
Create a service layer implementation class
public class AccountServicetImpl implements AccountService { //Injection dao layer interface private AccountDao accountDao; //To inject dependency in set mode, you need to provide the set method of dao layer public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public void delete(Integer id) { accountDao.delete(id); } public void update(Account account) { accountDao.update(account); } public List<Account> findAll() { return accountDao.findAll(); } public Account findById(Integer id) { return accountDao.findById(id); } }
spring configuration file ApplicationContext xml
<!--to configure service As spring of bean,injection dao--> <bean id="accountService" class="com.itheima.service.impl.AccountServicetImpl"> <property name="accountDao" ref="accountDao"/> </bean>
Because the injection dao layer interface has been written in AccountServicetImpl
Therefore, instead of declaring and injecting the dao into the bean, the ref attribute will be declared and injected into the bean automatically
However, no relevant configuration has been added yet. It needs to be done,
Running it now is to report an error
spring configuration file ApplicationContext xml
Add context space name and http path to its space name
xmlns:context="http://www.springframework.org/schema/context" Note: these two lines will report errors with red underscores This is because the quotation mark position is written incorrectly. These paths should be written in one quotation mark Is from xsi:schemaLocation= Started after http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
<!--load perperties Profile information--> <context:property-placeholder location="classpath:*.properties"/>
"classpath:*.properties is used to load all configuration files with the suffix properties
Then JDBC The information in the properties file is added to the spring configuration file
This is to use druid's function to create database links and establish database connection pool
Pay attention to the format ${} in the value attribute. If you don't take notes, you will always make mistakes
It says JDBC Name of the properties key
<!--load druid Resources are the resources of the database--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
This is the path between the alias in the mybatis core file and the dao interface of the mapped dao layer
Write to spring configuration file
The object and package name to be used can be found in the dependent path of coordinates
<!--spring integration mybatis Object for creating connection after control--> class It's about dependency mybatis-spring Medium SqlSessionFactoryBean Resource path SqlSessionFactoryBean Used for mybatis establish bean of <bean class="org.mybatis.spring.SqlSessionFactoryBean"> adopt ref Property to find dataSource Reference object function: create data resource connection objects <property name="dataSource" ref="dataSource"/> typeAliasesPackage mybatis Alias label in core file+Package label value Package path for alias <property name="typeAliasesPackage" value="com.itheima.domain"/> </bean>
These are the two sentences in the mybatis core file
<typeAliases> <package name="com.itheima.domain"/> </typeAliases>
This step is to implement the configuration of mybatis managed as a spring bean
With it, the dao layer can be associated with the SQL in the mybatis mapping file
<!--load mybatis Map configured scans as spring of bean Manage--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itheima.dao"/> </bean>
class="org.mybatis.spring.mapper.MapperScannerConfigurer"
This class writes the resource path of the mapping configuration scanner in the dependent coordinates
Is equal to this sentence
<mappers> <package name="com.itheima.dao"></package> </mappers>
Loading the scanning of mybatis mapping configuration and controlling after integrating mybatis with spring
Neither of these objects can write id
The reason is that it is used as an automatic proxy to create dao, and it is given to the spring configuration file
Spring can be used. It is for spring itself. Spring only needs to see this type
Now, if there is an error in the test, check whether the jdbc coordinates are written in the pom file
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency>
After writing these configurations, you can not use the mybatis core file
The version of the error prone point should be consistent with the version of spring context
Otherwise, it will report an error. It's hard to think of it
mybatis mapping configuration file is still required
For the convenience of later maintenance, write the id