美文网首页
JdbcTemplate

JdbcTemplate

作者: Fultom | 来源:发表于2019-12-04 11:20 被阅读0次

    JdbcTemplate

    • spiring 提供用于操作jdbc工具类,类似:DBUtils;
    • 依赖 连接池DataSource(数据源)

    1.1 搭建环境

    1.1.1 创建表

    create database ee19_spring_day02;
    use ee19_spring_day02;
    create table t_user(
        id int primark key auto_increment,
        username varchar(50),
        password varchar(32)
    )
    

    1.1.2 javabean

    public class User {
    
        private Integer id;
        private String username;
        private String password;
    

    1.2使用api

     //1.创建数据源(连接池)dbcp
            BasicDataSource dataSource = new BasicDataSource();
            //基本四项
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/ee19_spring_day02");
            dataSource.setUsername("root");
            dataSource.setPassword("123456");
    
    
            //2创建模板
            JdbcTemplate jdbcTemplate = new JdbcTemplate();
            jdbcTemplate.setDataSource(dataSource);
    
            //通过api操作   ]
            jdbcTemplate.update("insert into t_user(username,password) value (?,?);","tom","998");
        
    

    1.3 配置DBCP

    <!--创建数据源-->
        <bean id="dataSourceId" class="org.apache.commons.dbcp2.BasicDataSource">
            <property name="driverClassName" value="jdbc:mysql://localhost:3306/test"/>
            <property name="url" value="jdbc:mysql://localhost:3306/ee19_spring_day02"></property>
            <property name="username" value="root"/>
            <property name="password" value="12345678"/>
        </bean>
        <!--创建模板,需要注入数据源-->
        <bean id="jdbcTemplateId" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSourceId"></property>
        </bean>
    
        <!--配置dao-->
        <bean id="userDaoId" class="com.xft.c_dbcp.UserDao">
            <property name="jdbcTemplate" ref="jdbcTemplateId"/>
        </bean>
    

    1.4 配置c3p0

    <!-- c3p0 创建数据源-->
        <bean id="dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="dataSourceName" value="jdbc:mysql://localhost:3306/t_user"/>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ee19_spring_day02"></property>
            <property name="user" value="root"/>
            <property name="password" value="12345678"/>
        </bean>
        <!--创建模板,需要注入数据源-->
        <bean id="jdbcTemplateId" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSourceId"></property>
        </bean>
    
        <!--配置dao-->
        <bean id="userDaoId" class="com.xft.d_c3p0.UserDao">
            <property name="jdbcTemplate" ref="jdbcTemplateId"/>
        </bean>
    

    1.5

    1.5.1 dao层

    public class UserDao extends JdbcDaoSupport {
    
    
        public void update(User user){
            String sql = "update t_user set username=?,password=?,where id=?";
            Object[] args = {user.getUsername(),user.getPassword(),user.getId()};
            this.getJdbcTemplate().update(sql,args);
        }
    
        public List<User> findAll(){
    
            return this.getJdbcTemplate().query("select * from t_user",new BeanPropertyRowMapper<User>(User.class));
        }
    
    }
    
    

    1.5.2 spring配置文件

     <!--配置dao
            * dao 继承JdbcDaoSupport,之后只需要注入数据源,底层将自动创建模板
        -->
        <bean id="userDaoId" class="com.xft.e_jdbcdaosupport.UserDao">
            <property name="dataSource" ref="dataSourceId"/>
        </bean>
    

    1.6 properties

    properties文件的内容
    
    jdbc.dataSourceName=jdbc:mysql://localhost:3306/t_user
    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/t_user/ee19_spring_day02
    jdbc.user=root
    jdbc.password=12345678
    
    
     <!--加载配置文件
            classpath: 前缀表示src下
            在配置文件中通过${key}获取
        -->
        <context:property-placeholder location="classpath:com/xft/f_property/jdbcInfo.properties"/>
        <!-- c3p0 创建数据源-->
        <bean id="dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="dataSourceName" value="${jdbc.dataSourceName}"/>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
        <!--创建模板,需要注入数据源-->
        <bean id="jdbcTemplateId" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSourceId"></property>
        </bean>
    
        <!--配置dao
            * dao 继承JdbcDaoSupport,之后只需要注入数据源,底层将自动创建模板
        -->
        <bean id="userDaoId" class="com.xft.e_jdbcdaosupport.UserDao">
            <property name="dataSource" ref="dataSourceId"/>
        </bean>
    

    ·

    相关文章

      网友评论

          本文标题:JdbcTemplate

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