主頁 > 軟體設計 > SSH框架之Spring第四篇

SSH框架之Spring第四篇

2020-09-15 07:56:02 軟體設計

1.1 JdbcTemplate概述 :         它是spring框架中提供的一個物件,是對原始JdbcAPI物件的簡單封裝.spring框架為我們提供了很多的操作模板類.        ORM持久化技術                        模板類        JDBC                    org.springframework.jdbc.core.JdbcTemplate.        Hibernate3.0             org.springframework.orm.hibernate3.HibernateTemplate.        IBatis(MyBatis)            org.springframework.orm.ibatis.SqlMapClientTemplate.        JPA                        org.springframework.orm.jpa.JpaTemplate.    在導包的時候需要匯入spring-jdbc-4.24.RELEASF.jar,還需要匯入一個spring-tx-4.2.4.RELEASE.jar(它和事務有關)    <!-- 配置資料源 -->        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">            <property name="driverClassName" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"></property>            <property name="url" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:// /spring_day04"></property>            <property name="username" value="https://www.cnblogs.com/haizai/p/root"></property>            <property name="password" value="https://www.cnblogs.com/haizai/p/1234"></property>        </bean>    1.3.3.3配置spring內置資料源        spring框架也提供了一個內置資料源,我們也可以使用spring的內置資料源,它就在spring-jdbc-4.2.4.REEASE.jar包中:        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">            <property name="driverClassName" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"></property>            <property name="url" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_day04"></property>            <property name="username" value="https://www.cnblogs.com/haizai/p/root"></property>            <property name="password" value="https://www.cnblogs.com/haizai/p/1234"></property>        </bean>    1.3.4將資料庫連接的資訊配置到屬性檔案中:        【定義屬性檔案】        jdbc.driverClass=com.mysql.jdbc.Driver        jdbc.url=jdbc:mysql:///spring_day02        jdbc.username=root        jdbc.password=123        【引入外部的屬性檔案】        一種方式:            <!-- 引入外部屬性檔案: -->            <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">                <property name="location" value="https://www.cnblogs.com/haizai/p/classpath:jdbc.properties"/>            </bean>        二種方式:        <context:property-placeholder location="classpath:jdbc.properties"/>    1.4JdbcTemplate的增刪改查操作        1.4.1前期準備        創建資料庫:        create database spring_day04;        use spring_day04;        創建表:        create table account(            id int primary key auto_increment,            name varchar(40),            money float        )character set utf8 collate utf8_general_ci;    1.4.2在spring組態檔中配置JdbcTemplate        <?xml version="1.0" encoding="UTF-8"?>        <beans xmlns="http://www.springframework.org/schema/beans"                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                xsi:schemaLocation="http://www.springframework.org/schema/beans                 http://www.springframework.org/schema/beans/spring-beans.xsd">            <!-- 配置一個資料庫的操作模板:JdbcTemplate -->            <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">                <property name="dataSource" ref="dataSource"></property>            </bean>                        <!-- 配置資料源 -->            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">            <property name="driverClassName" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"></property>            <property name="url" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_day04"></property>            <property name="username" value="https://www.cnblogs.com/haizai/p/root"></property>            <property name="password" value="https://www.cnblogs.com/haizai/p/1234"></property>        </bean>        </beans>    1.4.3最基本使用        public class JdbcTemplateDemo2 {            public static void main(String[] args) {                //1.獲取Spring容器                ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");                //2.根據id獲取bean物件                JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");                //3.執行操作                jt.execute("insert into account(name,money)values('eee',500)");            }        }    1.4.4保存操作        public class JdbcTemplateDemo3 {            public static void main(String[] args) {                //1.獲取Spring容器                ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");                //2.根據id獲取bean物件                JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");                //3.執行操作                //保存                jt.update("insert into account(name,money)values(?,?)","fff",5000);            }        }    1.4.5更新操作        public class JdbcTemplateDemo3 {            public static void main(String[] args) {                //1.獲取Spring容器                ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");                //2.根據id獲取bean物件                JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");                //3.執行操作                //修改                jt.update("update account set money = money-? where id = ?",300,6);            }        }    1.4.6洗掉操作        public class JdbcTemplateDemo3 {            public static void main(String[] args) {                //1.獲取Spring容器                ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");                //2.根據id獲取bean物件                JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");                //3.執行操作                //洗掉                jt.update("delete from account where id = ?",6);            }        }    1.4.7查詢所有操作        public class JdbcTemplateDemo3 {            public static void main(String[] args) {                //1.獲取Spring容器                ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");                //2.根據id獲取bean物件                JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");                //3.執行操作                //查詢所有                List<Account> accounts = jt.query("select * from account where money > ? ",                                                     new AccountRowMapper(), 500);                for(Account o : accounts){                    System.out.println(o);                }            }        }        public class AccountRowMapper implements RowMapper<Account>{            @Override            public Account mapRow(ResultSet rs, int rowNum) throws SQLException {                Account account = new Account();                account.setId(rs.getInt("id"));                account.setName(rs.getString("name"));                account.setMoney(rs.getFloat("money"));                return account;            }                    }    1.4.8查詢一個操作        使用RowMapper的方式:常用的方式        public class JdbcTemplateDemo3 {            public static void main(String[] args) {                //1.獲取Spring容器                ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");                //2.根據id獲取bean物件                JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");                //3.執行操作                //查詢一個                List<Account> as = jt.query("select * from account where id = ? ",                                                 new AccountRowMapper(), 55);                System.out.println(as.isEmpty()?"沒有結果":as.get(0));            }        }    1.4.9查詢回傳一行一列操作        public class JdbcTemplateDemo3 {            public static void main(String[] args) {                //1.獲取Spring容器                ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");                //2.根據id獲取bean物件                JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");                //3.執行操作                //查詢回傳一行一列:使用聚合函式,在不使用group by字句時,都是回傳一行一列,最長用的就是分頁中獲取總記錄條數                Integer total = jt.queryForObject("select count(*) from account where money > ? ",Integer.class,500);                System.out.println(total);            }        }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"            xmlns:aop="http://www.springframework.org/schema/aop"            xmlns:tx="http://www.springframework.org/schema/tx"            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/aop            http://www.springframework.org/schema/aop/spring-aop.xsd            http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx.xsd">            <!-- 使用Spring管理連接池物件,Spring內置的連接池物件 -->                        <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">                <property name="driverClassName" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"></property>                <property name="url" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_04"/>                <property name="username" value="https://www.cnblogs.com/haizai/p/root"></property>                <property name="password" value="https://www.cnblogs.com/haizai/p/root"></property>            </bean> -->                        <!-- 配置資料源,使用Spring整合dbcp連接,沒有匯入jar包 -->            <!-- <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">                <property name="driverClassName" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"/>                <property name="url" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_04"/>                <property name="username" value="https://www.cnblogs.com/haizai/p/root"/>                <property name="password" value="https://www.cnblogs.com/haizai/p/root"/>            </bean> -->                        <!-- 使用Spring整合c3p0的連接池,沒有采用屬性檔案的方式 -->            <!--             <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">                <property name="driverClass" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"/>                <property name="jdbcUrl" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_04"/>                <property name="user" value="https://www.cnblogs.com/haizai/p/root"/>                <property name="password" value="https://www.cnblogs.com/haizai/p/root"/>            </bean> -->                        <!-- 使用context:property-placeholder標簽,讀取屬性檔案 -->            <context:property-placeholder location="classpath:db.properties"/>                        <!-- 使用Spring整合c3p0的連接池,采用屬性檔案的方式 -->            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">                <property name="driverClass" value="https://www.cnblogs.com/haizai/p/${jdbc.driver}"/>                <property name="jdbcUrl" value="https://www.cnblogs.com/haizai/p/${jdbc.url}"/>                <property name="user" value="https://www.cnblogs.com/haizai/p/${jdbc.user}"/>                <property name="password" value="https://www.cnblogs.com/haizai/p/${jdbc.password}"/>                            </bean>            <!-- spring管理JbdcTemplate模板 -->            <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">                <property name="dataSource" ref="dataSource"/>            </bean>        </beans>db.properties : 屬性檔案        jdbc.driver=com.mysql.jdbc.Driver        jdbc.url=jdbc:mysql:///spring_day04        jdbc.user=root        jdbc.password=rootDemo測驗 :    package com.ithiema.demo1;    import java.sql.ResultSet;    import java.sql.SQLException;    import java.util.List;    import javax.annotation.Resource;    import org.junit.Test;    import org.junit.runner.RunWith;    import org.springframework.jdbc.core.JdbcTemplate;    import org.springframework.jdbc.core.RowMapper;    import org.springframework.test.context.ContextConfiguration;    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;    /**     * Spring整合JdbcTemplate的方式入門     * @author Administrator     */    @RunWith(SpringJUnit4ClassRunner.class)    @ContextConfiguration("classpath:applicationContext.xml")    public class Demo11 {                @Resource(name="jdbcTemplate")        private JdbcTemplate jdbcTemplate;                /**         * 添加         */        @Test        public void run1(){            jdbcTemplate.update("insert into account values (null,?,?)", "嘿嘿",10000);        }                /**         * 修改         */        @Test        public void run2(){            jdbcTemplate.update("update account set name = ?,money = ? where id = ?", "嘻嘻",5000,6);        }                /**         * 洗掉         */        @Test        public void run3(){            jdbcTemplate.update("delete from account where id = ?", 6);        }                /**         * 查詢多條資料         */        @Test        public void run4(){            // sql          sql陳述句            // rowMapper    提供封裝資料的介面,自己提供實作類(自己封裝資料的)            List<Account> list = jdbcTemplate.query("select * from account", new BeanMapper());            for (Account account : list) {                System.out.println(account);            }        }        }    /**     * 自己封裝的實作類,封裝資料的     * @author Administrator     */    class BeanMapper implements RowMapper<Account>{                /**         * 一行一行封裝資料的         */        public Account mapRow(ResultSet rs, int index) throws SQLException {            // 創建Account物件,一個屬性一個屬性賦值,回傳物件            Account ac = new Account();            ac.setId(rs.getInt("id"));            ac.setName(rs.getString("name"));            ac.setMoney(rs.getDouble("money"));            return ac;        }            }2,1 Spring 事務控制我們要明確的    1: JavaEE體系進行分層開發,事務處理位于業務層,Spring提供了分層設計業務層的事務處理解決方案.    2: Spring框架為我們提供就一組事務控制的介面.這組介面是在spring-tx-4.2.4RELEASE.jar中.    3: spring的事務都是基于AOP的,它既可以使用編程的方式實作,也可以使用配置的方式實作    2.2Spring中事務控制的API介紹        2.2.1PlatformTransactionManager        此介面是spring的事務管理器,它里面提供了我們常用的操作事務的方法,如下圖:        我們在開發中都是使用它的實作類,如下圖:        真正管理事務的物件        org.springframework.jdbc.datasource.DataSourceTransactionManager    使用Spring JDBC或iBatis 進行持久化資料時使用        org.springframework.orm.hibernate3.HibernateTransactionManager        使用Hibernate版本進行持久化資料時使用    2.2.2TransactionDefinition        它是事務的定義資訊物件,里面有如下方法:    2.2.2.1事務的隔離級別    2.2.2.2事務的傳播行為        REQUIRED:如果當前沒有事務,就新建一個事務,如果已經存在一個事務中,加入到這個事務中,一般的選擇(默認值)        SUPPORTS:支持當前事務,如果當前沒有事務,就以非事務方式執行(沒有事務)        MANDATORY:使用當前的事務,如果當前沒有事務,就拋出例外        REQUERS_NEW:新建事務,如果當前在事務中,把當前事務掛起,        NOT_SUPPORTED:以非事務方式執行操作,如果當前存在事務,就把當前事務掛起        NEVER:以非事務方式運行,如果當前存在事務,拋出例外        NESTED:如果當前存在事務,則在嵌套事務內執行,如果當前沒有事務,則執行REQUIRED類似的操作,        2.2.2.3超時時間        默認值是-1,沒有超時限制,如果有,以秒為單位進行設定,        2.2.2.4是否是只讀事務        建議查詢時設定為只讀,        2.2.3TransactionStatus        此介面提供的是事務具體的運行狀態,方法介紹如下圖:    2.3基于XML的宣告式事務控制(配置方式)重點        2.3.1環境搭建        2.3.1.1第一步:拷貝必要的jar包到工程的lib目錄    2.3.1.2第二步:創建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:aop="http://www.springframework.org/schema/aop"             xmlns:tx="http://www.springframework.org/schema/tx"                xsi:schemaLocation="http://www.springframework.org/schema/beans                         http://www.springframework.org/schema/beans/spring-beans.xsd                        http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx.xsd                        http://www.springframework.org/schema/aop                         http://www.springframework.org/schema/aop/spring-aop.xsd">            </beans>        2.3.1.3第三步:準備資料庫表和物體類        創建資料庫:        create database spring_day04;        use spring_day04;        創建表:        create table account(            id int primary key auto_increment,            name varchar(40),            money float        )character set utf8 collate utf8_general_ci;        /**         * 賬戶的物體         */        public class Account implements Serializable {            private Integer id;            private String name;            private Float money;            public Integer getId() {                return id;            }            public void setId(Integer id) {                this.id = id;            }            public String getName() {                return name;            }            public void setName(String name) {                this.name = name;            }            public Float getMoney() {                return money;            }            public void setMoney(Float money) {                this.money = money;            }            @Override            public String toString() {                return "Account [id=" + id + ", name=" + name + ", money=" + money + "]";            }        }        2.3.1.4第四步:撰寫業務層介面和實作類        /**         * 賬戶的業務層介面         */        public interface IAccountService {                        /**             * 根據id查詢賬戶資訊             * @param id             * @return             */            Account findAccountById(Integer id);//                        /**             * 轉賬             * @param sourceName    轉出賬戶名稱             * @param targeName        轉入賬戶名稱             * @param money            轉賬金額             */            void transfer(String sourceName,String targeName,Float money);//增刪改        }        /**         * 賬戶的業務層實作類         */        public class AccountServiceImpl implements IAccountService {                        private IAccountDao accountDao;                        public void setAccountDao(IAccountDao accountDao) {                this.accountDao = accountDao;            }            @Override            public Account findAccountById(Integer id) {                return accountDao.findAccountById(id);            }            @Override            public void transfer(String sourceName, String targeName, Float money) {                //1.根據名稱查詢兩個賬戶                Account source = accountDao.findAccountByName(sourceName);                Account target = accountDao.findAccountByName(targeName);                //2.修改兩個賬戶的金額                source.setMoney(source.getMoney()-money);//轉出賬戶減錢                target.setMoney(target.getMoney()+money);//轉入賬戶加錢                //3.更新兩個賬戶                accountDao.updateAccount(source);                int i=1/0;                accountDao.updateAccount(target);            }        }        2.3.1.5第五步:撰寫Dao介面和實作類        /**         * 賬戶的持久層介面         */        public interface IAccountDao {                        /**             * 根據id查詢賬戶資訊             * @param id             * @return             */            Account findAccountById(Integer id);            /**             * 根據名稱查詢賬戶資訊             * @return             */            Account findAccountByName(String name);                        /**             * 更新賬戶資訊             * @param account             */            void updateAccount(Account account);        }        /**         * 賬戶的持久層實作類         * 此版本dao,只需要給它的父類注入一個資料源         */        public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {            @Override            public Account findAccountById(Integer id) {                List<Account> list = getJdbcTemplate().query("select * from account where id = ? ",new AccountRowMapper(),id);                return list.isEmpty()?null:list.get(0);            }            @Override            public Account findAccountByName(String name) {                List<Account> list =  getJdbcTemplate().query("select * from account where name = ? ",new AccountRowMapper(),name);                if(list.isEmpty()){                    return null;                }                if(list.size()>1){                    throw new RuntimeException("結果集不唯一,不是只有一個賬戶物件");                }                return list.get(0);            }            @Override            public void updateAccount(Account account) {                getJdbcTemplate().update("update account set money = ? where id = ? ",account.getMoney(),account.getId());            }        }        /**         * 賬戶的封裝類RowMapper的實作類         */        public class AccountRowMapper implements RowMapper<Account>{            @Override            public Account mapRow(ResultSet rs, int rowNum) throws SQLException {                Account account = new Account();                account.setId(rs.getInt("id"));                account.setName(rs.getString("name"));                account.setMoney(rs.getFloat("money"));                return account;            }        }        2.3.1.6第六步:在組態檔中配置業務層和持久層對        <!-- 配置service -->        <bean id="accountService" class="com.baidu.service.impl.AccountServiceImpl">            <property name="accountDao" ref="accountDao"></property>        </bean>                    <!-- 配置dao -->        <bean id="accountDao" class="com.baidu.dao.impl.AccountDaoImpl">            <!-- 注入dataSource -->            <property name="dataSource" ref="dataSource"></property>        </bean>                    <!-- 配置資料源 -->        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">            <property name="driverClassName" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"></property>            <property name="url" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_day04"></property>            <property name="username" value="https://www.cnblogs.com/haizai/p/root"></property>            <property name="password" value="https://www.cnblogs.com/haizai/p/1234"></property>        </bean>    2.3.2配置步驟        2.3.2.1第一步:配置事務管理器        <!-- 配置一個事務管理器 -->        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">            <!-- 注入DataSource -->            <property name="dataSource" ref="dataSource"></property>        </bean>        2.3.2.2第二步:配置事務的通知參考事務管理器        <!-- 事務的配置 -->        <tx:advice id="txAdvice" transaction-manager="transactionManager">        </tx:advice>        2.3.2.3第三步:配置事務的屬性        <!--在tx:advice標簽內部 配置事務的屬性 -->        <tx:attributes>        <!-- 指定方法名稱:是業務核心方法             read-only:是否是只讀事務,默認false,不只讀,            isolation:指定事務的隔離級別,默認值是使用資料庫的默認隔離級別,             propagation:指定事務的傳播行為,            timeout:指定超時時間,默認值為:-1,永不超時,            rollback-for:用于指定一個例外,當執行產生該例外時,事務回滾,產生其他例外,事務不回滾,沒有默認值,任何例外都回滾,            no-rollback-for:用于指定一個例外,當產生該例外時,事務不回滾,產生其他例外時,事務回滾,沒有默認值,任何例外都回滾,            -->            <tx:method name="*" read-only="false" propagation="REQUIRED"/>            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>        </tx:attributes>        2.3.2.4第四步:配置AOP-切入點運算式        <!-- 配置aop -->        <aop:config>            <!-- 配置切入點運算式 -->            <aop:pointcut expression="execution(* com.baidu.service.impl.*.*(..))" id="pt1"/>        </aop:config>        2.3.2.5第五步:配置切入點運算式和事務通知的對應關系        <!-- 在aop:config標簽內部:建立事務的通知和切入點運算式的關系 -->        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>        2.4基于XML和注解組合使用的整合方式        2.4.1環境搭建        2.4.1.1第一步:拷貝必備的jar包到工程的lib目錄        2.4.1.2第二步:創建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:aop="http://www.springframework.org/schema/aop"                    xmlns:tx="http://www.springframework.org/schema/tx"                    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/tx                             http://www.springframework.org/schema/tx/spring-tx.xsd                            http://www.springframework.org/schema/aop                             http://www.springframework.org/schema/aop/spring-aop.xsd                            http://www.springframework.org/schema/context                        http://www.springframework.org/schema/context/spring-context.xsd">            <!-- 配置spring要掃描的包 -->            <context:component-scan base-package="com.baidu"></context:component-scan>        </beans>    2.4.1.3第三步:創建資料庫表和物體類        和基于xml的配置相同,        2.4.1.4第四步:創建業務層介面和實作類并使用注解讓spring管理        業務層介面和基于xml配置的時候相同,略        /**         * 賬戶的業務層實作類         */        @Service("accountService")        public class AccountServiceImpl implements IAccountService {            @Autowired            private IAccountDao accountDao;            @Override            public Account findAccountById(Integer id) {                return accountDao.findAccountById(id);            }            @Override            public void transfer(String sourceName, String targeName, Float money) {                //1.根據名稱查詢兩個賬戶                Account source = accountDao.findAccountByName(sourceName);                Account target = accountDao.findAccountByName(targeName);                //2.修改兩個賬戶的金額                source.setMoney(source.getMoney()-money);//轉出賬戶減錢                target.setMoney(target.getMoney()+money);//轉入賬戶加錢                //3.更新兩個賬戶                accountDao.updateAccount(source);                int i=1/0;                accountDao.updateAccount(target);            }        }    2.4.1.5第五步:創建Dao介面和實作類并使用注解讓spring管理        Dao層介面和AccountRowMapper與基于xml配置的時候相同,略        @Repository("accountDao")        public class AccountDaoImpl implements IAccountDao {            @Autowired            private JdbcTemplate jdbcTemplate;                        @Override            public Account findAccountById(Integer id) {                List<Account> list = jdbcTemplate.query("select * from account where id = ? ",new AccountRowMapper(),id);                return list.isEmpty()?null:list.get(0);            }            @Override            public Account findAccountByName(String name) {                List<Account> list =  jdbcTemplate.query("select * from account where name = ? ",new AccountRowMapper(),name);                if(list.isEmpty()){                    return null;                }                if(list.size()>1){                    throw new RuntimeException("結果集不唯一,不是只有一個賬戶物件");                }                return list.get(0);            }            @Override            public void updateAccount(Account account) {                jdbcTemplate.update("update account set money = ? where id = ? ",account.getMoney(),account.getId());            }        }    2.4.2配置步驟        2.4.2.1第一步:配置資料源和JdbcTemplate        <!-- 配置資料源 -->        <bean id="dataSource"                     class="org.springframework.jdbc.datasource.DriverManagerDataSource">            <property name="driverClassName" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"></property>            <property name="url" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_day04"></property>            <property name="username" value="https://www.cnblogs.com/haizai/p/root"></property>            <property name="password" value="https://www.cnblogs.com/haizai/p/1234"></property>        </bean>    2.4.2.2第二步:配置事務管理器并注入資料源        <!-- 配置JdbcTemplate -->        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">            <property name="dataSource" ref="dataSource"></property>        </bean>    2.4.2.3第三步:在業務層使用@Transactional注解        @Service("accountService")        @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)        public class AccountServiceImpl implements IAccountService {                        @Autowired            private IAccountDao accountDao;            @Override            public Account findAccountById(Integer id) {                return accountDao.findAccountById(id);            }            @Override            @Transactional(readOnly=false,propagation=Propagation.REQUIRED)            public void transfer(String sourceName, String targeName, Float money) {                //1.根據名稱查詢兩個賬戶                Account source = accountDao.findAccountByName(sourceName);                Account target = accountDao.findAccountByName(targeName);                //2.修改兩個賬戶的金額                source.setMoney(source.getMoney()-money);//轉出賬戶減錢                target.setMoney(target.getMoney()+money);//轉入賬戶加錢                //3.更新兩個賬戶                accountDao.updateAccount(source);                //int i=1/0;                accountDao.updateAccount(target);            }        }        該注解的屬性和xml中的屬性含義一致,該注解可以出現在介面上,類上和方法上,        出現介面上,表示該介面的所有實作類都有事務支持,        出現在類上,表示類中所有方法有事務支持        出現在方法上,表示方法有事務支持,        以上三個位置的優先級:方法>類>介面    2.4.2.4第四步:在組態檔中開啟spring對注解事務的支持        <!-- 開啟spring對注解事務的支持 -->        <tx:annotation-driven transaction-manager="transactionManager"/>         2.5基于純注解的宣告式事務控制(配置方式)重點        2.5.1環境搭建        2.5.1.1第一步:拷貝必備的jar包到工程的lib目錄        2.5.1.2第二步:創建一個類用于加載spring的配置并指定要掃描的包        /**         * 用于初始化spring容器的配置類         */        @Configuration        @ComponentScan(basePackages="com.baidu")        public class SpringConfiguration {        }    2.5.1.3第三步:創建資料庫表和物體類        和基于xml的配置相同,略    2.5.1.4第四步:創建業務層介面和實作類并使用注解讓spring管理        業務層介面和基于xml配置的時候相同,略        /**         * 賬戶的業務層實作類         */        @Service("accountService")        public class AccountServiceImpl implements IAccountService {            @Autowired            private IAccountDao accountDao;            @Override            public Account findAccountById(Integer id) {                return accountDao.findAccountById(id);            }            @Override            public void transfer(String sourceName, String targeName, Float money) {                //1.根據名稱查詢兩個賬戶                Account source = accountDao.findAccountByName(sourceName);                Account target = accountDao.findAccountByName(targeName);                //2.修改兩個賬戶的金額                source.setMoney(source.getMoney()-money);//轉出賬戶減錢                target.setMoney(target.getMoney()+money);//轉入賬戶加錢                //3.更新兩個賬戶                accountDao.updateAccount(source);                int i=1/0;                accountDao.updateAccount(target);            }        }    2.5.1.5第五步:創建Dao介面和實作類并使用注解讓spring管理        Dao層介面和AccountRowMapper與基于xml配置的時候相同,略        @Repository("accountDao")        public class AccountDaoImpl implements IAccountDao {            @Autowired            private JdbcTemplate jdbcTemplate;                        @Override            public Account findAccountById(Integer id) {                List<Account> list = jdbcTemplate.query("select * from account where id = ? ",new AccountRowMapper(),id);                return list.isEmpty()?null:list.get(0);            }            @Override            public Account findAccountByName(String name) {                List<Account> list =  jdbcTemplate.query("select * from account where name = ? ",new AccountRowMapper(),name);                if(list.isEmpty()){                    return null;                }                if(list.size()>1){                    throw new RuntimeException("結果集不唯一,不是只有一個賬戶物件");                }                return list.get(0);            }            @Override            public void updateAccount(Account account) {                jdbcTemplate.update("update account set money = ? where id = ? ",account.getMoney(),account.getId());            }        }    2.5.2配置步驟        2.5.2.1第一步:使用@Bean注解配置資料源        @Bean(name = "dataSource")            public DataSource createDS() throws Exception {                DriverManagerDataSource dataSource = new DriverManagerDataSource();                dataSource.setUsername("root");                dataSource.setPassword("123");                dataSource.setDriverClassName("com.mysql.jdbc.Driver");                dataSource.setUrl("jdbc:mysql:///spring3_day04");                return dataSource;            }    2.5.2.2第二步:使用@Bean注解配置配置事務管理器        @Bean        public PlatformTransactionManager                 createTransactionManager(@Qualifier("dataSource") DataSource dataSource) {            return new DataSourceTransactionManager(dataSource);        }        2.5.2.3第三步:使用@Bean注解配置JdbcTemplate        @Bean        public JdbcTemplate createTemplate(@Qualifier("dataSource") DataSource dataSource)         {            return new JdbcTemplate(dataSource);        }    2.5.2.4第四步:在需要控制事務的業務層實作類上使用@Transactional注解        @Service("accountService")        @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)        public class AccountServiceImpl implements IAccountService {                        @Autowired            private IAccountDao accountDao;            @Override            public Account findAccountById(Integer id) {                return accountDao.findAccountById(id);            }            @Override            @Transactional(readOnly=false,propagation=Propagation.REQUIRED)            public void transfer(String sourceName, String targeName, Float money) {                //1.根據名稱查詢兩個賬戶                Account source = accountDao.findAccountByName(sourceName);                Account target = accountDao.findAccountByName(targeName);                //2.修改兩個賬戶的金額                source.setMoney(source.getMoney()-money);//轉出賬戶減錢                target.setMoney(target.getMoney()+money);//轉入賬戶加錢                //3.更新兩個賬戶                accountDao.updateAccount(source);                //int i=1/0;                accountDao.updateAccount(target);            }        }        該注解的屬性和xml中的屬性含義一致,該注解可以出現在介面上,類上和方法上,        出現介面上,表示該介面的所有實作類都有事務支持,        出現在類上,表示類中所有方法有事務支持        出現在方法上,表示方法有事務支持,        以上三個位置的優先級:方法>類>介面,    2.5.2.5第五步:使用@EnableTransactionManagement開啟spring對注解事務的的支持        @Configuration        @EnableTransactionManagement        public class SpringTxConfiguration {            //里面配置資料源,配置JdbcTemplate,配置事務管理器,在之前的步驟已經寫過了,        }在Spring中開啟事務的案例    applicationContext3.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"            xmlns:aop="http://www.springframework.org/schema/aop"            xmlns:tx="http://www.springframework.org/schema/tx"            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/aop            http://www.springframework.org/schema/aop/spring-aop.xsd            http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx.xsd">                        <!-- 使用Spring整合c3p0的連接池,沒有采用屬性檔案的方式 -->            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource ">                <property name="driverClass" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"/>                <property name="jdbcUrl" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_04"/>                <property name="user" value="https://www.cnblogs.com/haizai/p/root"/>                <property name="password" value="https://www.cnblogs.com/haizai/p/root"/>            </bean>                        <!-- 配置平臺事務管理器 -->            <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">                <property name="dataSource" ref="dataSource"></property>            </bean>            <!-- 配置通知: 是Spring框架提供通知,不是咋們自己撰寫的 -->            <tx:advice id="myAdvice" transaction-manager="transactionManager">                <tx:attributes>                    <!-- 給具體的業務層的方法進行隔離級別,傳播行為的具體的配置 -->                    <tx:method name="pay" isolation="DEFAULT" propagation="REQUIRED"/>                    <tx:method name="save*" isolation="DEFAULT"></tx:method>                    <tx:method name="find*" read-only="true"></tx:method>                </tx:attributes>            </tx:advice>            <!-- 配置AOP的增強 -->            <aop:config>                <!-- Spring框架制作的通知,必須要使用該標簽,如果是自定義的切面,使用aop:aspect標簽 -->                <aop:advisor advice-ref="myAdvice" pointcut="execution(public * com.baidu.*.*ServiceImpl.*(..))"></aop:advisor>            </aop:config>                        <!-- 可以注入連接池 -->            <bean id="accountDao" class="com.baidu.demo3.AccountDaoImpl">                <property name="dataSource" ref="dataSource"></property>            </bean>            <!-- 管理service -->            <bean id="accountService" class="com.baidu.demo3.AccountServiceImpl">                <property name="accountDao" ref="accountDao"></property>            </bean>                    </beans>在dao層對代碼進行了優化,優化了JdbcTemplate    public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {        /*    private JdbcTemplate jdbcTemplate;        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {        this.jdbcTemplate = jdbcTemplate;    }*/    //減錢    @Override    public void outMoney(String out, double money) {        //jdbcTemplate.update("update username set money = money - ? where name = ?",money,out);        this.getJdbcTemplate().update("update username set money = money - ? where name=?",money,out);    }    //加錢    @Override    public void inMoney(String in, double money) {        //jdbcTemplate.update("update username set money = money + ? where name = ?",money,in);        this.getJdbcTemplate().update("update username set money = money + ? where name=?",money,in);    }}Xml和注解一起進行Spring的事務管理        applicationContext4.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"                xmlns:aop="http://www.springframework.org/schema/aop"                xmlns:tx="http://www.springframework.org/schema/tx"                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/aop                http://www.springframework.org/schema/aop/spring-aop.xsd                http://www.springframework.org/schema/tx                 http://www.springframework.org/schema/tx/spring-tx.xsd">                                <!-- 開啟注解的掃描 -->                <context:component-scan base-package="com.baidu"/>                <!-- 使用Spring整合c3p0的連接池,沒有采用屬性檔案的方式 -->                <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">                    <property name="driverClass" value="https://www.cnblogs.com/haizai/p/com.mysql.jdbc.Driver"/>                    <property name="jdbcUrl" value="https://www.cnblogs.com/haizai/p/jdbc:mysql:///spring_04"/>                    <property name="user" value="https://www.cnblogs.com/haizai/p/root"/>                    <property name="password" value="https://www.cnblogs.com/haizai/p/root"/>                </bean>                                <!-- 配置JdbcTemplate模板 -->                <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">                    <property name="dataSource" ref="dataSource"></property>                </bean>                <!-- 配置平臺事務管理器 -->                <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">                    <property name="dataSource" ref="dataSource"></property>                </bean>                <!-- 開啟事務注解 -->                <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>                            </beans>Service層用注解 :    //實作類        @Service("accountService")        @Transactional(isolation=Isolation.DEFAULT)        public class AccountServiceImpl implements AccountService {            @Resource(name="accountDao")            private AccountDao accountDao;        //    public void setAccountDao(AccountDao accountDao) {        //        this.accountDao = accountDao;        //    }            //支付的方法            @Override            public void pay(String out, String in, double money) {                //模擬兩個操作                //減錢                accountDao.outMoney(out, money);                //模擬例外                //int i = 10/0;                accountDao.inMoney(in, money);            }        }使用純注解的方式進行Spring事務管理 :        /*         * 配置類,Spring宣告式事務管理,純注解的方式         *          */        @Configuration        @ComponentScan(basePackages="com.baidu.demo5")        @EnableTransactionManagement    //純注解的方式,開啟事務注解        public class SpringConfig {            @Bean(name="dataSource")            public DataSource createDataSource() throws Exception{                ComboPooledDataSource dataSource = new ComboPooledDataSource();                dataSource.setDriverClass("com.mysql.jdbc.Driver");                dataSource.setJdbcUrl("jdbc:mysql:///spring_04");                dataSource.setUser("root");                dataSource.setPassword("root");                                return dataSource;            }                        //把dataSource注入進來            @Bean(name="jdbcTemplate")            @Resource(name="dataSource")            public JdbcTemplate createJdbcTemplate(DataSource dataSource) {                return new JdbcTemplate(dataSource);            }                        //創建平臺事務管理器物件            @Bean(name="transactionManager")            @Resource(name="dataSource")            public PlatformTransactionManager createTransactionManager(DataSource dataSource) {                return new DataSourceTransactionManager(dataSource);            }                    }1 : 傳播行為 : 解決業務層方法之間互相呼叫的問題.    傳播行為的默認值 : 保證save和update方法在同一個事務中.2 : Spring事務管理 : (1) : XML組態檔 ; (2) : XML+注解組態檔 ; (3) : 純注解    Spring框架提供了介面和實作類,進行事務管理的.        PlatformTransactionManager介面 : 平臺事務管理器,提供和回滾事務的.            HibernateTransactionManager : hibernate框架事務管理的實作類.            DataSourceTransactionManager : 使用JDBC或者MyBattis框架        TransactionDefinition介面 : 事務的定義的資訊.提供很多常量,分別表示隔離級別,傳播行為.            傳播行為 : 事務的傳播行為,解決service方法之間的呼叫的問題.            Spring宣告式事務管理,使用AOP技術進行事務管理.        通知/增強 : 事務管理的方法,不用咋們自己撰寫.需要配置.                連接池 DataSource ,存在Connection ,使用JDBC進行事務管理 ,conn.commit()                        |                        |注入        平臺事務管理器(必須配置的),是Spring提供介面,提交和回滾事務                        |                        |注入        自己撰寫通知方法(事務管理),Spring提供通知,配置通知                        |                        |注入                配置AOP增強 aop:config

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/44341.html

標籤:設計模式

上一篇:觀察者模式

下一篇:設計模式-結構型-橋接模式

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:20:47 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:20:25 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:20:17 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:20:10 more
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:19:44 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:19:07 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:18:57 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:18:49 more
  • 05單件模式

    #經典的單件模式 public class Singleton { private static Singleton uniqueInstance; //一個靜態變數持有Singleton類的唯一實體。 // 其他有用的實體變數寫在這里 //構造器宣告為私有,只有Singleton可以實體化這個類! ......

    uj5u.com 2023-04-19 08:42:51 more
  • 【架構與設計】常見微服務分層架構的區別和落地實踐

    軟體工程的方方面面都遵循一個最基本的道理:沒有銀彈,架構分層模型更是如此,每一種都有各自優缺點,所以請根據不同的業務場景,并遵循簡單、可演進這兩個重要的架構原則選擇合適的架構分層模型即可。 ......

    uj5u.com 2023-04-19 08:42:41 more