发表于: 2019-10-28 21:11:02

1 1121


今天完成的事情:继续学习spring 任务17 JDBCTemplate 云服务器搭建环境(安装JDK)

jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/xiuzhenyuan?useUnicode=true&characterEncoding=utf8

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: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/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!--使用context命名空间,通过spring扫描指定包,导入数据库配置文件
   用来读取db.properties文件中的数据。-->
   <context:property-placeholder location="db.properties"/>

   <!--配置数据源  ,这里数据实现类来自dbcp中的一个属性类。其中属性的值就是来自于db.properties-->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${jdbc.driverClass}"></property>
       <property name="url" value="${jdbc.jdbcUrl}"></property>
       <property name="username" value="${jdbc.user}"></property>
       <property name="password" value="${jdbc.password}"></property>
   </bean>

   <!--配置JDBC模板  配置一个JdbcTemplate实例,并注入一个dataSource数据源-->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <property name="dataSource" ref="dataSource"></property>
   </bean>

</beans>

测试

ublic class JDBCTest1 {

ApplicationContext ctx new ClassPathXmlApplicationContext("applicationContext.xml");
  //获取IOC容器中的jdbcTemplate实例
   JdbcTemplate jdbcTemplate = (JdbcTemplatectx.getBean("jdbcTemplate");

   //插入数据
   @Test
   public void testInsert (){
String sql "insert into regist_info values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
       int count jdbcTemplate.update(sql,new Object[]{null,2023,"白深","22399227 ","java工程师","2019-10-01","北宋梁山强盗技术学校","www.jd.com","替天行道","晁盖","知乎",20190705,null});
       System.out.println(count);
   }

//删除数据
   @Test
   public void testDelete (){
String sql1 "delete from regist_info where id = ?";
       int count1 jdbcTemplate.update(sql1,16);
       System.out.println(count1);
   }

//修改数据
   @Test
   public void testUpdate (){
String sql2 "update regist_info set name = ? where id = ?";
       int count2 jdbcTemplate.update(sql2,"吴勇",22);
       System.out.println(count2);
   }

//查询单条数据
   @Test
   public void testSelect(){
String sql3 "select id,online_id onlineId,name,qq,carrer,enter_date enterDate,school,daily_links dailyLinks,slogan,senior,source,create_at createAt,update_at updateAt from regist_info where id = ?";
       RowMapper<StudentrowMapper new BeanPropertyRowMapper<Student>(Student.class);
       Student student = (StudentjdbcTemplate.queryForObject(sql3,rowMapper,5);
       System.out.println(student);

   }
//查询所有数据
   @Test
   public void testSelectAll(){
String sql3 "select id,online_id onlineId,name,qq,carrer,enter_date enterDate,school,daily_links dailyLinks,slogan,senior,source,create_at createAt,update_at updateAt from regist_info";
       RowMapper<StudentrowMapper new BeanPropertyRowMapper<Student>(Student.class);
       List<Studentstudents jdbcTemplate.query(sql3,rowMapper);
       for(Student studentstudents){
System.out.println(student);
       }

                          
明天计划的事情:在云服务器部署前面学过的服务                           
遇到的问题:NoSuchMethodError; 解决办法:springJAR包导错了                           
收获:

BeanFactory和ApplicationContext有什么区别?

BeanFactory 可以理解为含有bean集合的工厂类。BeanFactory 包含了种bean的定义,以便在接收到客户端请求时将对应的bean实例化。

BeanFactory还能在实例化对象的时生成协作类之间的关系。此举将bean自身与bean客户端的配置中解放出来。BeanFactory还包含了bean生命周期的控制,调用客户端的初始化方法(initialization methods)和销毁方法(destruction methods)。

从表面上看,application context如同bean factory一样具有bean定义、bean关联关系的设置,根据请求分发bean的功能。但application context在此基础上还提供了其他的功能。

  1. 提供了支持国际化的文本消息
  2. 统一的资源文件读取方式
  3. 已在监听器中注册的bean的事件

以下是三种较常见的 ApplicationContext 实现方式:

1、ClassPathXmlApplicationContext:从classpath的XML配置文件中读取上下文,并生成上下文定义。应用程序上下文从程序环境变量中取得。

ApplicationContext context = new ClassPathXmlApplicationContext(“bean.xml”);

2、FileSystemXmlApplicationContext :由文件系统中的XML配置文件读取上下文。

ApplicationContext context = new FileSystemXmlApplicationContext(“bean.xml”);

3、XmlWebApplicationContext:由Web应用的XML文件读取上下文。





返回列表 返回列表
评论

    分享到