-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.5version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.3.13version>
- dependency>
-
-
- "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 http://www.springframework.org/schema/beans/spring-beans.xsd">
-
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/db02"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- bean>
-
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- bean>
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.example.Mapper"/>
- bean>
-
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
- <property name="empMapper" ref="empMapper">property>
- <property name="userDAO" ref="userDAO"/>
- <property name="name" value="hhhh"/>
- bean>
-
- <bean name="userDAO" class="com.example.DAO.Impl.UserDAOImpl"/>
- beans>
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "https://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/db02"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- dataSource>
- environment>
- environments>
-
- <mappers>
- <package name="com.example.Mapper"/>
- mappers>
- configuration>
- package com.example.Test;
-
-
- import com.example.Service.UserService;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestApplicationContext {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- UserService UserServiceBean = (UserService) context.getBean(UserService.class);
- UserServiceBean.show();
- }
- }
-
接口实现类UserServiceImpl
- package com.example.Service.Impl;
-
- import com.example.DAO.UserDAO;
- import com.example.Mapper.EmpMapper;
- import com.example.Service.UserService;
- import com.example.pojo.Emp;
-
- import java.util.List;
-
-
- public class UserServiceImpl implements UserService {
- public EmpMapper empMapper;
-
- public UserServiceImpl() {
- System.out.println("UserService对象创建");
- }
-
- private UserDAO userDAO;
- private String name;
-
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void setUserDAO(UserDAO userDAO) {
- System.out.println("UserService执行注入UserDAO的操作:setDAO方法");
- this.userDAO = userDAO;
- }
-
- @Override
- public void show() {
- List
all = empMapper.findAll(); - for (Emp emp : all) {
- System.out.println(emp);
- }
- }
-
- public void setEmpMapper(EmpMapper empMapper) {
- this.empMapper = empMapper;
- }
- }
上述接口实现类的代码中,将Mapper接口类的对象直接进行注入(与配置文件中的配置相对应)
运行测试代码结果如下