首页 > 数据库技术 > 详细

Spring学习笔记(五)----Web项目整合、JdbcTemplate

时间:2019-09-02 01:17:12      阅读:140      评论:0      收藏:0      [点我收藏+]

Spring 整合 Web 项目

ApplicationContext 类处于 Action 层,每产生一个 Action 会存在效率问题

实现原理

  1. ServletContext 对象(只有唯一的一个对象)
  2. 监听器

    在服务器启动时,为每个项目创建一个 ServletContext 对象
    监听到 ServeletContext 创建时,加载 Spring 配置文件并创建配置好的对象
    将对象放到 ServeletContext 对象中 (setArrtibute方法)
    获取对象(getAttribute方法)

    配置监听器

    需要导入 spring-web jar 包

// web.xml
<listener>
    <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>

指定 spring 配置文件位置

// web.xml
// 默认会找\WEB-INF\applicationContext.xml
<context-parm>
    <param-name> contextConfigLocation </param-name>
    <param-value> classpath:applicationContext.xml </param-value>
</context-parm>

Spring 的 JdbcTemplate 操作

Spring 对不同的持久化技术都进行了封装 (Dao 层技术)

准备

  1. 导入 Jar 包
    • spring-jdbc
    • spring-tx
    • jdbc(数据库驱动的 jar 包,如:mysql-connector)

      数据库操作

    // 1。 创建对象,设置数据库信息
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSourrce.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql:///test");
    dataSource.setUsername("root");
    dataSource.setPardword("root");
    // 2. 创建 jdbcTemplate 对象,设置数据源
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    // 3. 使用 jdbcTemplate 的方法进行操作
    // 增加数据
    String sql = "insert into table values(?,?)";
    int rows = jdbcTemplate.updae(sql, "Jeson", "100");
    System.out.println(rows);
    // 删除数据
    String sql = "delete from table where xx=?";
    int rows = jdbcTemplate.update(sql, "xxx");
    System.out.println(rows);
    // 修改数据
    String sql = "update table set xx=? where xx=?";
    int rows = jdbcTemplate.update(sql, "xxx", "xxx");
    System.out.println(rows);
    // 查询数据
    // 查询返回一个值
    String sql = "seletc count(*) from table";
    int count = jdbcTemplate.queryForObject(sql, Integer.class);
    System.out.println(count);
    // 查询返回对象
    class MyRowMapper implements RowMapper<User> {
     @Override
     public User mapRow(ResultSet rs, int num) throws SQLException {
         // 从结果集得到数据并封装到自己定义的类中
         String username = rs.getString("username");
         String password = rs.getString("password");
    
         User user = new User();
         user.setUseranme(username);
         user.setPassword(password);
         return user;
     }
    }
    String sql = "select * from table where xx=?";
    User user = jdbcTemplate.queryForObject(sql, new MyRowMapper(), "xx");
    System.out.println(user);
    // 查询返回 List
    String sql = "select * from talbe";
    List list = jdbcTemplate.query(sql, new MyRowMapper());
    Syste.out.println(list);

    Spring 配置连接池和 Dao 使用 JdbcTemplate

    Spring 配置 c3p0 连接池

    jar包:c3p0 和 machange-commons-java
    ~xml








    ~

    Dao 使用 JdacTemplate

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     <!-- 注入数据源 -->
     <property name="dataSource" ref="dataSource"/>
    </bean>
    @Component(value="userDao")
    public class UserDao {
    
     @Autowired
     private JdbcTemplate jdbcTemplate;
     public void show() {
         String sql = "select * from talble";
         List list = jdbcTemplate.queryForByObject(sql, new MyRowMapper());
         // TODO
     }
    }

Spring学习笔记(五)----Web项目整合、JdbcTemplate

原文:https://www.cnblogs.com/qq188380780/p/11443848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!