12. Integrate Mybatis
step:
- Configure maven dependencies
- junit
- mybatis
- mysql database
- spring related
- aop weaving
- mybatis-spring [new package]
- Write configuration file
- test
12.1 Memories of mybatis
-
Writing entity classes
StudentMapper.xml
<?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.kuber.mapper.StudentMapper"> <select id="studentQuery" resultType="Student"> select * from student; </select> </mapper>
-
Write core configuration files
mybatis-config.xml file
<?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="db.properties"/> <typeAliases> <package name="com.kuber.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/kuber/mapper/StudentMapper.xml"/> </mappers> </configuration>
db.properties configuration file
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/bigdata?serverTimezone=UTC&userSSL=false&useUnicode=true&characterEncoding=UTF-8 username=root password=root
-
Write interface
StudentMapper interface class
public interface StudentMapper { public List<Student> studentQuery(); }
-
Write mapper.xml
StudentMapper.xml
<?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.kuber.mapper.StudentMapper"> <select id="studentQuery" resultType="Student"> select * from student; </select> </mapper>
-
test
@Test public void test1() throws IOException { String resources = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resources); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sessionFactory.openSession(true); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> students = mapper.studentQuery(); for (Student student : students) { System.out.println(student); } }
-
result
12.2、Mybatis-spring
-
Write data source configuration
-
sqlSessionFactory
-
sqlSessionTemplate
1-3 is integrated as follows, spring-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/bigdata?serverTimezone=UTC&userSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--绑定mybatis配置文件--> <property name="configLocation" value="mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/kuber/mapper/StudentMapper.xml"/> </bean> <!--SqlSessionTemplate:就是我们使用的sqlSession--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <!--老方法,重点掌握--> <bean id="studentMapper" class="com.kuber.mapper.StudentMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> <!--新方法,接口实现类中继承SqlSessionDaoSupport类--> <bean id="studentMapper2" class="com.kuber.mapper.StudentMapperImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>
-
Need to add implementation class to interface【】
The interface implementation class of the old method (focus on mastering):
public class StudentMapperImpl implements StudentMapper{ private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } @Override public List<Student> studentQuery() { StudentMapper mapper = sqlSessionTemplate.getMapper(StudentMapper.class); return mapper.studentQuery(); } }
The interface implementation class of the new method (this can be simpler)
public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper{ @Override public List<Student> studentQuery() { return getSqlSession().getMapper(StudentMapper.class).studentQuery(); } }
-
Inject your own implementation class into Spring
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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <!--老方法,重点掌握--> <bean id="studentMapper" class="com.kuber.mapper.StudentMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> <!--新方法,接口实现类中继承SqlSessionDaoSupport类--> <bean id="studentMapper2" class="com.kuber.mapper.StudentMapperImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>
-
Just test and use
Test of the old method:
@Test public void test2(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); StudentMapper studentMapper = applicationContext.getBean("studentMapper", StudentMapper.class); for (Student student : studentMapper.studentQuery()) { System.out.println(student); } }
Testing of the new method:
@Test public void test3(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); StudentMapper studentMapper = applicationContext.getBean("studentMapper2", StudentMapper.class); for (Student student : studentMapper.studentQuery()) { System.out.println(student); } }
-
result