Spring笔记(四)

关于Spring的整合

(1)Spring整合数据库

dbcp数据库连接池

第一步:导入jar包
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>
<dependency>
    <groupId>commons-dbutils</groupId>
    <artifactId>commons-dbutils</artifactId>
    <version>1.4</version>
</dependency>
第二步:配置beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解-->
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.sram"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
第三步:配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql:///test"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>
第四步:进行相关数据库操作
C3P0数据库连接池
maven坐标
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
配置文件
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
    <property name="user" value="root"></property>
    <property name="password" value="root"></property>
</bean>
Druid数据库连接池
maven坐标
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.18</version>
</dependency>
配置文件
<!-- 数据源,阿里Druid连接池 -->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql:///test" />
    <property name="username" value="root" />
    <property name="password" value="root" />
    <!-- 配置初始化大小、最小、最大 -->
    <property name="initialSize" value="5" />
    <property name="minIdle" value="1" />
    <property name="maxActive" value="300" />
    <!-- 配置获取连接等待超时的时间 -->
    <property name="maxWait" value="60000" />
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="300000" />
    <property name="validationQuery" value="SELECT 1" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <!-- PSCache,并且指定每个连接上PSCache的大小(mysql数据库建议关闭) -->
    <property name="poolPreparedStatements" value="false" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="-1" />
    <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
    <property name="filters" value="stat" />
</bean>
Spring内置数据源
maven坐标
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>
配置文件
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql:///test"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>

(2)Spring整合mybatis

整合mybatis

第一步:pom.xml导入依赖
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.5</version>
</dependency>

<!--整合mybatis与spring需要的jar包-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.1</version>
</dependency>
第二步:dao层的接口使用mybatis的注解或者xml配置

注解:

public interface UserDao {
    @Insert("INSERT INTO t_person (name,pwd) VALUES (#{name},#{pwd})")
    public void addUser(User user);
    @Select( "update t_person set name=#{name},pwd=#{pwd} where id=#{id}" )
    public void updateUser(User user);
}

xml配置:

<insert id="addUser" parameterType="com.xszx.beans.User">
    insert into t_person (name,pwd) values (#{name},#{pwd})
</insert>

<update id="updateUser" parameterType="com.xszx.beans.User">
    update t_person set name=#{name},pwd=#{pwd} where id=#{id}
</update>
第三步:业务层(Service层)
@Service("userService")
public class UserServiceImpl implements UserService{
    @Autowired
    @Qualifier("userDao")
    private UserDao userDao;
    public void addUser(User user) {
        userDao.addUser( user );
    }

    public void updateUser(User user) {
        return userDao.updateUser(user);
    }

}
第四步:beans.xml
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.sram"/>
 <context:property-placeholder location="classpath:jdbc_config.properties"/>
 <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql:///test"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>
<!--创建sqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--注入数据源对象-->
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!--扫描dao层接口的包, 创建动态代理对象, 存入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--需要指定dao层接口的包名-->
    <property name="basePackage" value="com.sram.dao"></property>
</bean>
第五步:测试
//创建spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
//获取容器中的对象
UserService userService =(UserService) ac.getBean( "userService");
System.out.println(userService.findAll());

整合mybatis.xml

前两步同上

第三步:beans.xml
<!--创建sqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--注入数据源对象-->
    <property name="dataSource" ref="dataSource"></property>
    <property name="mapperLocations" value="classpath:com/sram/beans/*.xml"></property>
    <property name="typeAliasesPackage" value="com.sram.beans"></property>
</bean>
<!--xml-->
<bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
第四步:业务层
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource(name="sessionTemplate")
    private SqlSessionTemplate sessionTemplate;
    public List<User> findAll()
    {
        UserDao userDao = sessionTemplate.getMapper( UserDao.class );
        return userDao.findAll();
    }
}
第五步:测试
@Test
public void test02(){
    //1.使用 ApplicationContext 接口,就是在获取 spring 容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    //2.根据 bean 的 id 获取对象
    UserService acd = (UserService) ac.getBean("userService");
    System.out.println(acd.findAll());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值