依赖
dependencies>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-jdbcartifactId>
version>5.1.9.RELEASEversion>
dependency>
dependency>
groupId>org.mybatisgro服务器托管网upId>
artifactId>mybatis-springartifactId>
version>2.0.2version>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatisartifactId>
version>3.5.2version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-contextartifactId>
version>5.1.9.RELEASEversion>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
version>5.1.47version>
dependency>
dependency>
groupId>org.aspectjgroupId>
artifactId>aspectjweaverartifactId>
version>1.8.13version>
dependency>
dependencies>
build>
resources>
resource>
directory>src/main/javadirectory>
includes>
include>**/*.propertiesinclude>
include>**/*.xmlinclude>
服务器托管网 includes>
filtering>truefiltering>
resource>
resource>
directory>src/main/resourcesdirectory>
includes>
include>**/*.propertiesinclude>
include>**/*.xmlinclude>
includes>
filtering>truefiltering>
resource>
resources>
build>
配置文件
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="org.springframework.jdbc.datasource.DriverManagerDataSource">
property name="driverClassName" value="com.mysql.jdbc.Driver"/>
property name="url" value="jdbc:mysql://localhost:3306/test?useSSL&useUnicode=true&charsetEncoding=UTF-8"/>
property name="username" value="root"/>
property name="password" value="root"/>
bean>
bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
property name="dataSource" ref="dataSource"/>
property name="configLocation" value="classpath:mybatis-config.xml"/>
property name="mapperLocations" value="classpath:org/example/mapper/*.xml"/>
bean>
bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
constructor-arg index="0" ref="sqlSessionFactory"/>
bean>
bean id="userMapperImpl" class="org.example.mapper.UserMapperImpl">
property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
bean>
bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
property name="dataSource" ref="dataSource"/>
bean>
tx:advice id="txAdvice" transaction-manager="transactionManager">
tx:attributes>
tx:method name="*" propagation="REQUIRED"/>
tx:method name="query" read-only="true"/>
tx:attributes>
tx:advice>
aop:config>
aop:pointcut id="txPointCut" expression="execution(* org.example.mapper.*.*(..))"/>
aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
aop:config>
beans>
mapper接口的实现
// 手动注入sqlSession或继承SqlSessionDaoSupport
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{
/*private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}*/
@Override
public ListUser> getUserList() {
// return sqlSessionTemplate.getMapper(UserMapper.class).getUserList();
return getSqlSession().getMapper(UserMapper.class).getUserList();
}
}
测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapperImpl", UserMapper.class);
userMapper.getUserList();
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: SpringBoot + LiteFlow:轻松应对复杂业务逻辑,简直不要太香!
LiteFlow简介 LiteFlow是什么? LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑。通过支持热加载规则配置,开发者能够即时调整流程步骤,将复杂的业务如价格计算、下单流程等拆分为独…