zoukankan      html  css  js  c++  java
  • Spring框架——JDBC方式搭建项目

    学习Spring框架,使用JDBC的方式配置方式搭建一个项目,进行项目总结。


    首先,采用MVC设计模式思想,搭建项目目录。

    这里写图片描述

    然后各个目录文件下面的相关源码附上:

    controller目录:

    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.jredu.jdbc.dao.AccountDao;
    import com.jredu.jdbc.entity.Account;
    public class AccountAction {
    
        public static void main(String[] args) {
    
            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-jdbc.xml");
            AccountDao dao = app.getBean(AccountDao.class);
            //System.out.println(dao.selectAll());
    
            //插入
    /*      Account account = new Account();
            account.setUname("admin2");
            account.setPwd("321");
            System.out.println(dao.insert(account));*/
    
            //更新修改
            /*Account account = new Account();
            account.setId(3);
            account.setUname("张三");
            account.setPwd("abc");
            System.out.println(dao.update(account));*/
    
            //查询
            //System.out.println(dao.selectAll());
    
            //删除
            //System.out.println(dao.delete(3));
    
            //查询一条
            System.out.println(dao.selectOne(2));
        }
    }

    dao层目录的源码:

    import java.util.List;
    
    import com.jredu.jdbc.entity.Account;
    
    public interface AccountDao {
    
        //查询一条
        Account selectOne(int id);
    
        //查询
        List<Account> selectAll();
    
        //添加
        int insert(Account account);
    
        //删除
        int delete(int id);
    
        //修改更新
        int update(Account account);
    
    }

    dao.impl目录下的源码:

    package com.jredu.jdbc.dao.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.stereotype.Repository;
    
    import com.jredu.jdbc.dao.AccountDao;
    import com.jredu.jdbc.entity.Account;
    
    @Repository
    public class AccountDaoImpl implements AccountDao{
    
        //我们自己注入进来的
        @Autowired
        private JdbcTemplate template;
    
        @Override
        public Account selectOne(int id) {
            // TODO Auto-generated method stub
            String sql = "select * from account where id=?";
            RowMapper<Account> mapper = new BeanPropertyRowMapper<Account>(Account.class);  
            return template.queryForObject(sql,mapper,id);
        }
    
        @Override
        public List<Account> selectAll() {
            // TODO Auto-generated method stub
    
            RowMapper<Account> mapper = new BeanPropertyRowMapper<Account>(Account.class);
            String sql = "select * from account";
            return template.query(sql,mapper);
        }
    
        @Override
        public int insert(Account account) {
            // TODO Auto-generated method stub
            String sql = "insert into account values(account_sql.nextval,?,?)";
            return template.update(sql, account.getUname(),account.getPwd());
        }
    
        @Override
        public int delete(int id) {
            // TODO Auto-generated method stub
            String sql = "delete from account where id=?";
            return template.update(sql,id);
        }
    
        @Override
        public int update(Account account) {
            // TODO Auto-generated method stub
            String sql = "update account set uname=?,pwd=? where id=?";
            return template.update(sql,account.getUname(),account.getPwd(),account.getId());
        }
    
    }
    

    entity实体类目录源码:

    package com.jredu.jdbc.entity;
    
    public class Account {
    
        private int id;
        private String uname;
        private String pwd;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        @Override
        public String toString() {
            return "Account [id=" + id + ", uname=" + uname + ", pwd=" + pwd
                    + ", getId()=" + getId() + ", getUname()=" + getUname()
                    + ", getPwd()=" + getPwd() + ", getClass()=" + getClass()
                    + ", hashCode()=" + hashCode() + ", toString()="
                    + super.toString() + "]";
        }
    
    
    
    }
    

    util目录下的源码:

    package com.jredu.jdbc.util;
    
    import javax.sql.DataSource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    public class BaseDao extends JdbcDaoSupport{
    
        //该方法自动被调用
        @Autowired
        public void setJdbcDataSource(DataSource datasource){
            setDataSource(datasource);
        }
        //调用的是bean,自动设置  则AccountDaoImpl接口方法 不需要private JdbcTemplate template;
    
    }
    

    然后,就要进行最重要的环节部分了,以下部分需要细致仔细的配置,否则很容易出错。

    第一步,导入导入c3p0的jar包,导入jdbc驱动包。 将这两个包导入WebRoot目录下的WEB—INF目录里的lib文件夹里。
    这里写图片描述

    第二步,在applicationContext-jdbc.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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:util="http://www.springframework.org/schema/util"
        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/util
            http://www.springframework.org/schema/util/spring-util.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 自动扫描 -->
        <context:component-scan base-package="com.jredu.jdbc"></context:component-scan>    
        <!-- 配置C3P0数据源 -->
        <!-- 导入资源文件 -->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <bean
            id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            p:user="${jdbc.user}"
            p:password="${jdbc.pwd}"
            p:driverClass="${jdbc.driverClassName}"
            p:jdbcUrl="${jdbc.url}"
            p:initialPoolSize="${jdbc.initPoolSize}"
            p:maxPoolSize="${jdbc.maxPoolSize}"
        />
        <!-- 配置JDBC Template -->
        <bean
            id="jdbcTemplate"
            class="org.springframework.jdbc.core.JdbcTemplate"
            p:dataSource-ref="dataSource"
        />
    
    </beans>

    第三步,配置jdbc-properties文件,由于我连接的Oracle,所以配置的是Oracle数据库。

    jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
    jdbc.url=jdbc:oracle:thin:@localhost:1521:jredu
    jdbc.user=OnlineTest
    jdbc.pwd=Jredu12345
    jdbc.initPoolSize=30
    jdbc.maxPoolSize=10
    jdbc.paper=A4
    jdbc.box=gray

    至此,项目搭建完毕,搭建完后如果需要添加其他功能,补充其他功能添加即可运行。

  • 相关阅读:
    Hashset 常用的方法
    List新增的方法
    关于集合
    转载关于sql 注入
    可变参数和preparestatement 结合使用
    数据库连接 中的测试事例,包括工具类,配置文件,查询,增加,以及查询后返回对象
    在静态方法中不能调用非静态变量,但getclass()可以换个形式来调用
    用一个对象中读出信息进行出入的代码片段
    新建maven项目更改 web版本
    elasticsearch 局部更新
  • 原文地址:https://www.cnblogs.com/aixing/p/13327661.html
Copyright © 2011-2022 走看看