标签:artifact 基础上 ons port sso session jdb pat tor
步骤:
MyBati-Spring会帮助你将MjBais 代码无缝地整合到spring 中。它将允许MVBais参与到Ssping的事务管理之中,创建映射器mapper和sga.1essicn并注入到cean中,以及将Mlypats的异常转换为Spring的DataAccessException。最终,可以做到应用代码不依赖于MyBatis,Spring或MyBatis-Spring.
导入jar包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
编写数据源配置
<!--DataSource-:使用Spring的数据源替换Mybatis的配置:C3P0、Druid-->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?UseUnicode=true&characterEncoding=utf-8" />
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
SqlSessionFactory
<!-- SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--引入外部mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--引入mapper.xml文件-->
<property name="mapperLocations" value="classpath:com/accp/mapper/*.xml"/>
</bean>
SqlSessoinTemplate
<!--SqlSession对象-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
需要给接口添加实现类
public class DeptMapperImpl implements DeptMapper{
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public List<Dept> selectDept() {
return sqlSessionTemplate.getMapper(DeptMapper.class).selectDept();
}
}
将自己写的实现类注册到Spring中测试使用
<bean id="deptMapper" class="com.accp.mapper.DeptMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
DeptMapperImpl deptMapper = context.getBean("deptMapper", DeptMapperImpl.class);
List<Dept> deptList = deptMapper.selectDept();
for (Dept dept : deptList) {
System.out.println(dept);
}
}
方式二简化了方式一的基础上简化了 private SqlSessionTemplate sqlSessionTemplate;对象在实现类中的注入
修改前:
public class DeptMapperImpl implements DeptMapper{
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public List<Dept> selectDept() {
return sqlSessionTemplate.getMapper(DeptMapper.class).selectDept();
}
}
beans.xml
<bean id="deptMapper" class="com.accp.mapper.DeptMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
修改后:
public class DeptMapperImpl extends SqlSessionDaoSupport implements DeptMapper{
public List<Dept> selectDept() {
return getSqlSession().getMapper(DeptMapper.class).selectDept();
}
}
beans.xml
<bean id="deptMapper" class="com.accp.mapper.DeptMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
标签:artifact 基础上 ons port sso session jdb pat tor
原文地址:https://www.cnblogs.com/ITRonion/p/13763043.html