1、引入依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.11</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.13</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
2、编写mapper接口
3、编写mapper配置文件
⼀定要注意,按照下图提示创建这个⽬录。注意是斜杠不是点⼉。在resources⽬录下新建。并且要和 Mapper接⼝包对应上
结构如下
如果接⼝叫做UserMapper,配置⽂件必须是UserMapper.xml
4、编写service接⼝和service接⼝实现类
注意编写的service实现类纳⼊IoC容器管理:
public interface UserService { int insert(User user); } @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public int insert(User user) { return userMapper.insert(user); } }
5、编写jdbc.properties配置⽂件
放在类的根路径下
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring6 jdbc.username=root jdbc.password=root
6、编写mybatis-config.xml配置⽂件
放在类的根路径下,只开启⽇志,其他配置到spring.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> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> </configuration>
7、编写spring.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--组件扫描--> <context:component-scan base-package="com.springcode"/> <!--外部属性配置⽂件--> <context:property-placeholder location="jdbc.properties"/> <!--数据源--> <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> <!--SqlSessionFactoryBean--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--mybatis核⼼配置⽂件路径--> <property name="configLocation" value="mybatis-config.xml"/> <!--注⼊数据源--> <property name="dataSource" ref="dataSource"/> <!--起别名--> <property name="typeAliasesPackage" value="com.springcode.example.entity.User"/> </bean> <!--Mapper扫描器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.springcode.example.mapper"/> </bean> <!--事务管理器--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--开启事务注解--> <tx:annotation-driven transaction-manager="txManager"/> </beans>
8、测试
public class SpringTest { @Test public void testInsert(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); UserService userService = applicationContext.getBean("userService", UserService.class); userService.insert(new User(1,"abc")); } }