图集导航
1 . 编写User
@Table(name = "user") public class User { @Id private Integer id ; private String name ; private Integer age ; private Double num ; //构造及其get/set方法省略 }
2.编写UserMapper
import com.czxy.demo22_xml_sw.daomain.User; import tk.mybatis.mapper.common.Mapper; //UserMapper接口直接继承Mapper接口,关联User类 public interface UserMapper extends Mapper<User> { }
3. 编写UserService接口及其实现类
—— UserService接口
import com.czxy.demo22_xml_sw.daomain.User; import java.util.List; public interface UserService { //查询所有 List<User> selectAll(); }
—— UserServiceImpl接口实现类
package com.czxy.demo22_xml_sw.service.impl; import com.czxy.demo22_xml_sw.daomain.User; import com.czxy.demo22_xml_sw.mapper.UserMapper; import com.czxy.demo22_xml_sw.service.UserService; import java.util.List; public class UserServiceImpl implements UserService { //依赖注入mapper private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } //查询所有 @Override public List<User> selectAll() { return userMapper.selectAll(); } }
4.properties文件
1. jdbc.driver=com.mysql.cj.jdbc.Driver 2. jdbc.url=jdbc:mysql://localhost:3306/day02_mysql 3. jdbc.username=root 4. jdbc.password=1234
5. 编写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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--1 数据源 --> <!--1.1 加载配置文件 加载properties文件--> <context:property-placeholder location="classpath:db.properties"/> <!--1.2 配置数据源 【连接数据库,获取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> <!--2 mapper --> <!--2.1 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--value=“JavaBean所在的daomain包的全路径” 可自行根据需要定义路径 --> <property name="typeAliasesPackage" value="com.czxy.demo22_xml_sw.daomain" /> <property name="configuration"> <bean class="org.apache.ibatis.session.Configuration"> <property name="mapUnderscoreToCamelCase" value="true" /> </bean> </property> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <props> <prop key="dialect">mysql</prop> <prop key="rowBoundsWithCount">true</prop> </props> </property> </bean> </array> </property> </bean> <!--2.2 配置mapper扫描--> <bean id="mapperScanner" class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- value= "mapper包所在的的全路径" 可自行根据需要定义路径 --> <property name="basePackage" value="com.czxy.demo22_xml_sw.mapper"/> </bean> <!--3.配置bean--> <!-- 3.1 service class="service实现类所在的全路径名" --> <bean id="userService" class="com.czxy.demo22_xml_sw.service.impl.UserServiceImpl"> <property name="userMapper" ref="userMapper"/> </bean> <!--4 事务相关--> <!--4.1 平台事务管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--4.2 事务属性配置(事务详情)--> <tx:advice id="txAdvice" transaction-manager="transactionManager" > <tx:attributes> <tx:method name="change" propagation="REQUIRED"/> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!--4.3 事务aop编程 --> <aop:config> <!-- expression = “切入点表达式” --> <aop:pointcut id="txPointcut" expression="execution(* com.czxy.demo22_xml_sw..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans>
5.1 配置文件中要根据自己的项目做一些修改的,图集展示
—— 修改properties文件的路径
—— 修改JavaBean所在的daomain包路径
——修改mapper所在包路径
—— 修改service实现类UserServiceImpl所在的全路径
—— 事务aop编程切入点表达式的修改
6.编写测试类
@RunWith(SpringRunner.class) // 加载配置类文件,如果是xml方式就加载xml文件; // 如果是注解方式,就加载配置类config包中的类 @ContextConfiguration(locations = "classpath:Demo22_spring_sw.xml") public class TestSM { @Resource private UserService userService; @Test public void testDemo22(){ List<User> list= userService.selectAll(); list.forEach(System.out::println); } }
7.测试结果