美文网首页Spring
spring dao层使用jdbcTemplte

spring dao层使用jdbcTemplte

作者: DouDouZH | 来源:发表于2018-06-07 17:59 被阅读7次

一、实现步骤

(1)配置c3p0链接池


image.png

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


image.png

(3)创建JdbcTemplate对象,把模版对象注入dao里面

  • 创建模版对象


    创建模版对象
  • 把模版对象注入进dao


    把模版对象注入进去

(4)向模版对象里注入dataSource


向模版对象里注入dataSource

二、代码

1、c3p0连接池配置

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/person
jdbc.user=root
jdbc.password=root
2、UserBean.java
package work.zhangdoudou.bean;

public class UserBean {
    private String uname;
    private String upwd;
    private String uquestion;
    private String uanswer;
    private int u_type;
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
    public String getUquestion() {
        return uquestion;
    }
    public void setUquestion(String uquestion) {
        this.uquestion = uquestion;
    }
    public String getUanswer() {
        return uanswer;
    }
    public void setUanswer(String uanswer) {
        this.uanswer = uanswer;
    }
    public int getU_type() {
        return u_type;
    }
    public void setU_type(int u_type) {
        this.u_type = u_type;
    }
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }
}
3、UserService.java
package work.zhangdoudou.Service;

import work.zhangdoudou.Dao.UserDao;
import work.zhangdoudou.bean.UserBean;

public class UserService {
    
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    //添加操作
    public void addUser(UserBean userBean) {
        userDao.addUser(userBean);
    }
    
}
4、UserDao.java
package work.zhangdoudou.Dao;

import org.springframework.jdbc.core.JdbcTemplate;

import work.zhangdoudou.bean.UserBean;

public class UserDao {
    
    //得到jdbcTemplate对象
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public void addUser(UserBean userBean) {
        String sql="INSERT INTO users(u_name,u_pwd) VALUES(?,?)";
        Object []date = {userBean.getUname(),userBean.getUpwd()};
        jdbcTemplate.update(sql,date);
    }
}
5、applicationContext.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: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-4.0.xsd">
    <!--  value 从配置文件里面 db.properties中取值 -->
    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}" ></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
    </bean> 
    
    <bean id="userBean" class="work.zhangdoudou.bean.UserBean" scope="prototype"></bean>
    
    <bean id="userService" class="work.zhangdoudou.Service.UserService">
        <!-- 注入dao的对象 -->
        <property name="userDao" ref="userDao"></property>
    </bean> 
    <bean id="userDao" class="work.zhangdoudou.Dao.UserDao">
        <!-- 注入jdbcTemplate -->
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>    
    
    <!-- 创建jdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- dataSource 传递到模板中 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
  
</beans>
6、TestUser.java
package work.zhangdoudou.Test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import work.zhangdoudou.Service.UserService;
import work.zhangdoudou.bean.UserBean;

public class TestUset {

    @Test
    public void test() {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService=(UserService)context.getBean("userService");
        UserBean userBean=(UserBean)context.getBean("userBean");
        userBean.setUname("zhangsan");
        userBean.setUpwd("123");
        userService.addUser(userBean);
    }

}

三、运行结果

image.png

相关文章

网友评论

    本文标题:spring dao层使用jdbcTemplte

    本文链接:https://www.haomeiwen.com/subject/ijvgsftx.html