spring第三部分(三)&.spring配置连接池

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39128354/article/details/81457703

Spring配置连接池和dao使用jdbcTemplate

1.spring配置c3po

第一步:
导入jar包: c3po-0.9.2.1.jar
                mchange-commons-java-0.2.3.4.jar

        ComboPooledDataSource datasource=new ComboPooledDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
                dataSource.setUrl("jdbc:mysql://localhost:3306/teachger_student");
                dataSource.setUsername("root");
                dataSource.setPassword("123456");
    第一步:把代码在配置文件中进行配置
<?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:context="http://www.springframework.org/schema/context" 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"

            <!--配置c3po-->
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource.ComboPooledDataSource()">    
            <!--配置属性-->
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///000></property>
            <property name=user" value="root"></property>
            <property name="password" value="123456"></property>
            </bean>
        </beans>
第二步:
java代码
        class UserDao{
            //得到jdbcTemplate对象
            private JdbcTemplate jdbcTemplate;

            public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
                this.jdbcTemplate = jdbcTemplate;
            }
            //添加操作
            public void add() {
                //创建jdbcTemplate对象
                String sql="insert into user value(?,?)";
                jdbcTemplate.update(sql,"11","nice");
            }

        }
        class UserServiec{
            private UserDao userDao;

            public void setUserDao(UserDao userDao) {
                this.userDao = userDao;
            }
            public void add() {

                userDao.add();
            }

创建spring配置文件,配置连接池

配置信息
        <?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:context="http://www.springframework.org/schema/context" 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"

            <!--配置c3po-->
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource.ComboPooledDataSource()">    
            <!--配置属性-->
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql:///000></property>
            <property name=username" value="root"></property>
            <property name="password" value="123456"></property>
            </bean>

(1)创建service和dao,配置service和dao对象,在service注入dao对象

<!--创建service和dao对象,在service注入dao-->
            <bean id="userService" class="_Bean.UserServiec">
                <property name="userDaop" ref="userService"></property>
            </bean>
            <bean id="userDao" class="_Bean.UserDao">

(2)创建jdbcTemplate对象,吧摸版对象注入到dao里面

    <bean id="userDao" class="_Bean.UserDao">
                <!--注入jdbcTemplate对象-->
                <property name="jdbcTemplate" ref="jdbcTemplate"></property>
            </bean>
            <bean id jdbcTemplate' class="org.springframework.jdbc.core.JdbcTemplarte">

(3)在jdbcTemplate对象;里面注入

private DataSource dataSource;
            private SQLExceptionTranslator exceptionTranslator;
            private boolean lazyInit=true;
            public void setDataSource(DataSource dataSource){
                this.dataSopurce=dataSource;
            }   
            <!--创建jdbcTemplate对象-->
            <bean id jdbcTemplate' class="org.springframework.jdbc.core.JdbcTemplarte">     
                <!-- 把DataSource传递到摸版对象里面-->
                <property name="dataSource" ref="dataSource"></property>            
            </bean>
        </beans>        

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试