• 【学习笔记】Spring JdbcTemplate (3-3-3)


    Spring与JDBC模板(jdbcTemplate)

      为了避免直接使用JDBC而带来的复杂冗长的代码

    Spring提供的一个强有力的模板类 -- jdbcTemplate
    简化JDBC操作

    并且数据源DataSource对象与模板JdbcTemplate对象

      都可以通过Bean的形式定义在配置文件中 充分发挥了依赖注入的威力

    1.jar包
    Spring jdbc jar包
    spring tx (事务jar包)

    2.导入命名空间
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"

    3.搭建分层

    //beans
            public class Book{
                private int bid;
                private String bname;
                private int bprice;
            }
            
            //dao
            public interface IBookDao{
                int addBook(Book book);
            }
                //impl
                public class BookDaoImpl implements IBookDao{
                    public int addBook(Book book){
                        
                        
                        return 0;
                    }
                }
                
                
            //service
            public interface IBookBiz{
                int addBook(Book book);
            }
                //impl
                public class BookBizImpl implements IBookBiz{
                    private IBookDao dao;
                    public int addBook(Book book){
    
                        return dao.addBook();
                    }
                }
    //xml配置
                //dao
                <bean id="bookdao" class="xx.xx.BookDaoImpl"></bean>
                
                //service
                <bean id="bookbiz" class="xx.xx.BookBizImpl">
                    <property name="dao" ref="bookdao"></property>
                </bean>

    4.注册数据源

    注册源分为三类 如下
    Spring   内置的   DriverManagerDataSource
    DBCP         BasicDataSource
    C3P0          ComboPooledDataSource

    //DriverManagerDataSource
            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverName" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql:///book"></property>
                <property name="username" value="root"></property>
                <property name="password" value="root"></property>
            </bean>
            
            //BasicDataSource
            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
                <property name="driverName" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql:///book"></property>
                <property name="username" value="root"></property>
                <property name="password" value="root"></property>
            </bean>
            
            //ComboPooledDataSource
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClasss" value="com.mysql.jdbc.Driver"></property>
                <property name="jdbcUrl" value="jdbc:mysql:///book"></property>
                <property name="user" value="root"></property>
                <property name="password" value="root"></property>
            </bean>
            
            
            
        4.jdbc的属性文件
            //jdbc.properties
            jdbc.driver=xxx
            jdbc.url=xxx
            jdbc.username=xxx
            jdbc.password=xxx
            
            //xml
                1.PropertyPlaceholderConfigurer
                
                2.<context:property-placeholder location="classpath:jdbc.properties"/>
            
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClasss" value="${jdbc.driver}"></property>
                <property name="jdbcUrl" value="${jdbc.url}"></property>
                <property name="user" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>
            </bean>
            
            1.PropertyPlaceholderConfigurer
            <bean class="PropertyPlaceholderConfigurer">
                <property name="location" value="classpath:jdbc.properties"></property>
            </bean>

    以上是XML中配置三种数据源 的方法

    以及使用properties文件配置的方法

    5.继承JdbcTemplate 实现新增一本图书

    public class BookDaoImpl extends JdbcDaoSupport implements IBookDao{
                public int addBook(Book book){
                    String sql="xxx";
                    return this.getJdbcTeplate().update(sql,book.getBname,book.getBprice);
    
                }
            }

    6.书写测试类

    @Test
        public void test1() {
            ApplicationContext ctx =
                    new ClassPathXmlApplicationContext("applicationContext.xml");
            IBookBiz biz = (IBookBiz) ctx.getBean("bookBiz");
            Book book = new Book();
            book.setBname("第二本书");
            book.setBprice(10);
    
            biz.add(book);
        }

    运行之后 成功在数据表中 插入了一条记录

    使用注解的方式实现使用JdbcTemplate连接数据库

    1.DAO IMPL

    @Repository
    public class BookDaoImpl implements IBookDao {
        @Resource
        private JdbcTemplate template;
    
        public int add(Book book) {
            String sql = "insert into book values(null,?,?)";
            int update = getTemplate().update(sql, book.getBname(), book.getBprice());
            return update;
        }
    
        public JdbcTemplate getTemplate() {
            return template;
        }
    
        public void setTemplate(JdbcTemplate template) {
            this.template = template;
        }
    }

    2.BIZ IMPL

    @Service("bookBiz")
    public class BookBizImpl implements IBookBiz {
        @Autowired
        private IBookDao dao;
    
        public int add(Book book) {
            return dao.add(book);
        }
    
        public IBookDao getDao() {
            return dao;
        }
    
        public void setDao(IBookDao dao) {
            this.dao = dao;
        }
    }

    3.XML配置

    <context:component-scan base-package="sword"></context:component-scan>
    
        <!--配置数据源-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql:///book"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>
    
        <!--jdbcTemplate-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
  • 相关阅读:
    Eureka Server的多级缓存和过期机制
    eureka-client拉取注册表
    Ribbon的调用流程
    EurekaServer启动
    eureka的注册
    Eureka的客户端是怎么启动的?
    Ribbon的负载均衡源码
    Ribbon是怎么重构URL的?
    Maven添加本地jar
    window 常用软件记录
  • 原文地址:https://www.cnblogs.com/swordtm/p/6644835.html
Copyright © 2020-2023  润新知