博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring框架基础(中)
阅读量:5377 次
发布时间:2019-06-15

本文共 12606 字,大约阅读时间需要 42 分钟。

  • Spring对不同持久化技术进行支持
    • JDBC
      • 导入spring-jdbc-4.3.5.RELEASE.jar、spring-tx-4.3.5.RELEASE.jar
      • 创建对象,设置数据库信息
      • 创建jdbcTemplate对象,设置数据源
      • 调用jdbcTemplate对象里面对方法实现操作
        public class TestDao {      /**      * 添加指定数据表的数据      */    public void insertUser() {        try {            //导入配置文件            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("db.properties");            Properties properties = new Properties();            properties.load(inputStream);            //获取参数            String driver = properties.getProperty("jdbc.driver");            String url = properties.getProperty("jdbc.url");            String username = properties.getProperty("jdbc.username");            String password = properties.getProperty("jdbc.password");            //            DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();            driverManagerDataSource.setJdbcUrl(driver);            driverManagerDataSource.setJdbcUrl(url);            driverManagerDataSource.setUser(username);            driverManagerDataSource.setPassword(password);            //数据库语句            String sql = "insert into user (username,password,email,root,register_time) values (?,?,?,?,?) ";            //            JdbcTemplate jdbcTemplate = new JdbcTemplate(driverManagerDataSource);            int rows = jdbcTemplate.update(sql,"rabbit","rabbit","rabbit@qq.com",1,"2019-3-2");            System.out.println("rows="+rows);        } catch (IOException e) {            e.printStackTrace();        }    }      /**      * 更新指定数据表的数据      */   public void updateUser() {        try {            //导入配置文件            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("db.properties");            Properties properties = new Properties();            properties.load(inputStream);            //获取参数            String driver = properties.getProperty("jdbc.driver");            String url = properties.getProperty("jdbc.url");            String username = properties.getProperty("jdbc.username");            String password = properties.getProperty("jdbc.password");            //            DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();            driverManagerDataSource.setJdbcUrl(driver);            driverManagerDataSource.setJdbcUrl(url);            driverManagerDataSource.setUser(username);            driverManagerDataSource.setPassword(password);            //数据库语句                     String sql = "update user set phone = ? where username = ?";            //            JdbcTemplate jdbcTemplate = new JdbcTemplate(driverManagerDataSource);                        int rows = jdbcTemplate.update(sql,"13812392132","rabbit");            System.out.println("rows=" + rows);        } catch (IOException e) {            e.printStackTrace();        }    }      /**      * 查询全部数据      */    public void selectAllUser() {        try {            //导入配置文件            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("db.properties");            Properties properties = new Properties();            properties.load(inputStream);            //获取参数            String driver = properties.getProperty("jdbc.driver");            String url = properties.getProperty("jdbc.url");            String username = properties.getProperty("jdbc.username");            String password = properties.getProperty("jdbc.password");            //            DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();            driverManagerDataSource.setJdbcUrl(driver);            driverManagerDataSource.setJdbcUrl(url);            driverManagerDataSource.setUser(username);            driverManagerDataSource.setPassword(password);            //数据库语句                      String sql = "select * from user";            //            JdbcTemplate jdbcTemplate = new JdbcTemplate(driverManagerDataSource);                      List
        > maps = jdbcTemplate.queryForList(sql); System.out.println("maps=" + maps); } catch (IOException e) { e.printStackTrace(); } } /** * 删除指定数据 */ public void deleteUser() { try { //导入配置文件 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取参数 String driver = properties.getProperty("jdbc.driver"); String url = properties.getProperty("jdbc.url"); String username = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); // DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); driverManagerDataSource.setJdbcUrl(driver); driverManagerDataSource.setJdbcUrl(url); driverManagerDataSource.setUser(username); driverManagerDataSource.setPassword(password); //数据库语句 String sql = "delete from user where username = ?"; // JdbcTemplate jdbcTemplate = new JdbcTemplate(driverManagerDataSource); int row = jdbcTemplate.update(sql, "rabbit"); System.out.println("row=" + row); } catch (IOException e) { e.printStackTrace(); } } /** * 查询指定数据表的记录数 */ public void selectCountUser() { try { //导入配置文件 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取参数 String driver = properties.getProperty("jdbc.driver"); String url = properties.getProperty("jdbc.url"); String username = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); // DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); driverManagerDataSource.setJdbcUrl(driver); driverManagerDataSource.setJdbcUrl(url); driverManagerDataSource.setUser(username); driverManagerDataSource.setPassword(password); //数据库语句 String sql = "select count(*) from user"; // JdbcTemplate jdbcTemplate = new JdbcTemplate(driverManagerDataSource); //第一个参数sql语句,第二个参数返回的数据类型 Integer count = jdbcTemplate.queryForObject(sql,Integer.class); System.out.println("count=" + count); } catch (IOException e) { e.printStackTrace(); } } public void selectUser();() { try { //导入配置文件 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取参数 String driver = properties.getProperty("jdbc.driver"); String url = properties.getProperty("jdbc.url"); String username = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); // DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); driverManagerDataSource.setJdbcUrl(driver); driverManagerDataSource.setJdbcUrl(url); driverManagerDataSource.setUser(username); driverManagerDataSource.setPassword(password); //数据库语句 String sql = "select * from user where username = ?"; User user = jdbcTemplate.queryForObject(sql, new NewRowMapper(), "rabbit"); JSONObject jsonObject = JSONObject.fromObject(user); System.out.println(jsonObject + ""); } catch (IOException e) { e.printStackTrace(); } } /** * 处理查询结果集 */ class NewRowMapper implements RowMapper
        { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { try { Class
        newClass = Class.forName("cn.muriel.auto.pojo.User"); Object o = newClass.newInstance(); for (int j = 1; j <= resultSet.getMetaData().getColumnCount(); j++) { String name = resultSet.getMetaData().getColumnName(j); String type = resultSet.getMetaData().getColumnTypeName(j); //针对只有一次_的情况,多次则可用递归 if (name.contains("_")) { int position = name.indexOf("_"); String smailChar = name.substring(position + 1, position + 2).toUpperCase(); name = name.substring(0, position) + smailChar + name.substring(position + 2, name.length()); } Field declaredField = newClass.getDeclaredField(name); if (declaredField != null) { declaredField.setAccessible(true); if (type.equals("INT")) declaredField.set(o, resultSet.getInt(name)); else if (type.equals("VARCHAR")) declaredField.set(o, resultSet.getString(name)); } } return (User) o; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } return null; } } }/*** 测试代码*/ public static void main(String[] args) { Test01 test01 = (Test01) applicationContext.getBean("test01"); test01.test01();*/ TestDao dao = new TestDao(); dao.insertUser(); dao.updateUser(); dao.selectAllUser(); dao.selectUser(); dao.selectCountUser();}
      • spring配置c3p0连接池
        • 导入mchange-commons-java-0.2.11.jar、c3p0-0.9.5.2.jar
        • 连接连接池
          • 代码实现
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("db.properties");        Properties properties = new Properties();        try {            properties.load(inputStream);            //获取参数            String driver = properties.getProperty("jdbc.driver");            String url = properties.getProperty("jdbc.url");            String username = properties.getProperty("jdbc.username");            String password = properties.getProperty("jdbc.password");            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();            comboPooledDataSource.setDriverClass(driver);            comboPooledDataSource.setJdbcUrl(url);            comboPooledDataSource.setUser(username);            comboPooledDataSource.setPassword(password);        } catch (IOException e) {            e.printStackTrace();        } catch (PropertyVetoException e) {            e.printStackTrace();        }

             

          • 注入实现

             

    • Hibernate
    • Ibatis(MyBatis)
    • JPA
  • Spring的事务管理
    • 什么是事务
    • 事务的特性
    • 不考虑隔离性产生读问题
    • 解决读问题
      • 设置隔离级别
    • spring事务管理两种方式
      • 编程式事务管理
      • 声明式事务管理
        • 基于xml配置文件实现

           

        • 基于注解实现
          /** * 使用事务的方法所在类上面添加注解 */@Transactionalpublic class TestService { }

           

    • spring事务管理高层抽象主要包括3个接口
      • PlatformTransactionManager(事务管理器)
        • spring针对不同的dao层框架,提供接口不同的实现类
        • 首先都要配置事务管理器
      • TransactionDefinition(事务定义信息(隔离、传播、超时、只读))
      • TransactionStatus(事务具体运行状态)

转载于:https://www.cnblogs.com/fatRabbit-/p/10562544.html

你可能感兴趣的文章
海量存储——致性和高可用专题
查看>>
让div里面的两个元素竖直排列,并相对于其水平垂直居中
查看>>
XmlDocument操作
查看>>
循环结构
查看>>
团队开发spring会议~day6
查看>>
net 购物车实现代码参照
查看>>
Linux study
查看>>
PHP smarty
查看>>
[day8]Python学习之接口开发
查看>>
android studio lint 静态检查
查看>>
redis分布式锁
查看>>
Docker的安装配置及使用详解
查看>>
Filter代码解析
查看>>
还行,多少学了点东西,不是啥都没学到
查看>>
win7 清灰引发的惨案
查看>>
ExpandStackTrace
查看>>
每个Xcode开发者应该知道的几个使用技巧
查看>>
安卓模拟器添加图片
查看>>
【转】25岁到55岁:如何规划人生最重要的三个十年
查看>>
yii去掉自动排序功能
查看>>