Spr + mybatis implementation step diagram
1. Configure the environment and add jar packages
jar package mainly includes ioc, aop, dao, connection pool dbcp, mybatis-spring, mybatis driver package.
When I study, I find that all the configuration and code are correct, but the operation will be wrong, the problem lies in the version of the jar package. Different versions of jar packages may cause errors, including jdk versions. So you guys should pay attention when practicing.
Configuration of applicationContext.xml
The application Context. XML file contains:
Step 6, Configure DataSource DBCP
Step 7, configure SqlSession FactoryBean
Step 8, configure MapperScanner Configurer
<?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" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- Definition dbcp Of DataSource --> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="root"></property> <property name="password" value="asdf123456"></property> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean> <!-- Establish sqlSessionFactory --> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- injection dataSource --> <property name="dataSource" ref="dbcp"> </property> <!-- injection SQL Statement file --> <property name="mapperLocations" value="classpath:cn/springmybatis02/sql/*.xml"> </property> </bean> <!-- According to the given Mapper Interface Generation Implementation Component --> <!-- Appoint mapper Interface --> <!-- Appoint sqlsession Resources --> <!-- <bean id="empDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="cn.springmybatis02.dao.EmpDao"> </property> <property name="sqlSessionFactory" ref="ssf"> </property> </bean> --> <!-- Batch generation can be based on the specified path Dao Realization --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- scanning cn.springmybatis02.dao Pack all interfaces,Implementation of batch generation --> <property name="basePackage" value="cn.springmybatis02.dao"> </property> <!-- This can be omitted and injected automatically. sqlsessionfactory --> <!-- <property name="sqlSessionFactory" ref="ssf"> </property> --> </bean> </beans>
III. Writing Entity Classes
According to their actual situation, it is necessary to write entity classes. The role of entity classes in this learning note exists as the return value type of sql.
package cn.springmybatis02.entity; import java.io.Serializable; public class Emp implements Serializable{ public Emp() { } public Emp(String name, Double salary) { super(); this.name = name; this.salary = salary; } public Emp(String name, Integer sex, Double salary) { super(); this.name = name; this.sex = sex; this.salary = salary; } private Integer id; private String name; private Integer sex; private Double salary; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } }
IV. Defining SQL Files
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="cn.springmybatis02.dao.EmpDao"> <select id="findAll" resultType="cn.springmybatis02.entity.Emp"> select * from emp </select> </mapper>
Defining dao interface
package cn.springmybatis02.dao; import java.util.List; import cn.springmybatis02.entity.Emp; public interface EmpDao { public List<Emp> findAll(); }
VI. Writing Test Classes
package cn.springmybatis02.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.springmybatis02.dao.EmpDao; import cn.springmybatis02.entity.Emp; public class EmpDaoTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDao empDao = ac.getBean("empDao",EmpDao.class); List<Emp> list = empDao.findAll(); for(Emp emp : list) { System.out.println("name:"+emp.getName()+", sex:"+emp.getSex()+", salary:"+emp.getSalary()); } } }
7. Other knowledge, custom annotation markers
1. Custom Annotation Markup Class
package cn.springmybatis02.annotation; /** * This class is an annotated definition class. * Represents that a function can only be implemented with the @MyBatisDao annotation * */ public @interface MyBatisDao { }
2. Define the place of use
Adding restrictions to the generation of dao interface implementations, only adding @MyBatisDao annotation tag class before dao interface class can generate dao interface implementations.
<?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" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- Definition dbcp Of DataSource --> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="root"></property> <property name="password" value="asdf123456"></property> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean> <!-- Establish sqlSessionFactory --> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- injection dataSource --> <property name="dataSource" ref="dbcp"> </property> <!-- injection SQL Statement file --> <property name="mapperLocations" value="classpath:cn/springmybatis02/sql/*.xml"> </property> </bean> <!-- According to the given Mapper Interface Generation Implementation Component --> <!-- Appoint mapper Interface --> <!-- Appoint sqlsession Resources --> <!-- <bean id="empDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="cn.springmybatis02.dao.EmpDao"> </property> <property name="sqlSessionFactory" ref="ssf"> </property> </bean> --> <!-- Batch generation can be based on the specified path Dao Realization --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- scanning cn.springmybatis02.dao Pack all interfaces,Implementation of batch generation --> <property name="basePackage" value="cn.springmybatis02.dao"> </property> <!-- Only classes with custom annotation tags can be generated Dao Realization --> <property name="annotationClass" value="cn.springmybatis02.annotation.MyBatisDao"> </property> <!-- This can be omitted and injected automatically. sqlsessionfactory --> <!-- <property name="sqlSessionFactory" ref="ssf"> </property> --> </bean> </beans>
3. Adding Annotation Markers
Add a custom annotation tag @MyBatis dao to the class that needs to generate a dao interface implementation
package cn.springmybatis02.dao; import java.util.List; import cn.springmybatis02.annotation.MyBatisDao; import cn.springmybatis02.entity.Emp; @MyBatisDao public interface EmpDao { public List<Emp> findAll(); }
4. Testing
Test again with the test class in step 6.