maven中的操作
其余操作就跟mybatis操作是一样的
<properties>
<!--在这个标记下配置常量-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>5.1.5.RELEASE</spring.version>
<!--常量版本太多容易管理定义一个常量只要跟这个包版本一致找这个常量即可-->
</properties>
—————————————————————————————
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<!--使用常量通过常量名来调用-->
</dependency>
需要的jar:
数据数据源c3p0
Spring包
mybatis包
xml配置:注意Spring的xml文件不要和mybatis的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:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context/spring-context.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">
<!--注解配置-->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.sun.SpringAndMybatis"></context:component-scan>
<!--事务管理员-->
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--数据库链接-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test2?useUnicode=true&
characterEncoding=UTF-8&useSSL=false"/>
<property name="user" value="root"/>
<property name="password" value="Sb1996350."/>
<property name="maxPoolSize" value="3"/>
<property name="minPoolSize" value="1"/>
</bean>
<!--创建mybatis对象工厂sqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--数据源对象 将dataSource对象放入、属性dataSource中-->
<!--相应的配置找里面的属性进行配置即可-->
<!--mybatis配置的xml路径-->
<property name="mapperLocations" value="classpath:mybatis/*.xml"></property>
</bean>
<!--扫描接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描mapper映射接口-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
<!--value通过名字来查找sqlSessionFactory对象-->
</property>
<property name="basePackage" value="com.sun.SpringAndMybatis.Dao"/>
</bean>
</beans>
其余操作就跟mybatis操作是一样的
package com.sun.SpringAndMybatis;
import com.sun.SpringAndMybatis.Dao.Stu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml");
Test test = applicationContext.getBean(Test.class);
Stu stu = test.Select(5);
System.out.println(stu.toString());
}
}
@Component
@Transactional
public class Test {
@Autowired
private StuMapper stuMapper;//引用接口 通过标记将对象赋值给他
public void rgeUser(Integer sid, String sname, String address) {
Stu stu = new Stu();
stu.setAddress(address);
stu.setSname(sname);
stuMapper.insertSelective(stu);
}
public Stu Select(Integer sid) {
Stu stu = stuMapper.selectByPrimaryKey(sid);
return stu;
}
}