发表于: 2017-12-23 18:38:44

1 527


2017.12.22日报:


一。今天确定了ref属性:查找当前配置文件里的bean

ref标签有3种属性:

<ref bean=""/>  :查找全局中的bean。即可以查找其他xml配置文件的bean,优先从当前配置文件寻找

<ref local=""/>   :查找当前配置文件中bean,等同于ref属性。local属性值必须同目标bean的id属性值相同

<ref parent=""/>:指定其依赖的父JavaBean定义


二。做JDBCTemplate

首先配置各种资源pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.ptteng</groupId>
   <artifactId>JdbcTemplateTest</artifactId>
   <version>1.0-SNAPSHOT</version>

   <dependencies>

       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <scope>test</scope>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.3.12.RELEASE</version>
       </dependency>

       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.45</version>
       </dependency>

       <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>4.3.13.RELEASE</version>
       </dependency>
       <!-- https://mvnrepository.com/artifact/com.alexkasko.springjdbc/springjdbc-iterable -->
       <dependency>
           <groupId>com.alexkasko.springjdbc</groupId>
           <artifactId>springjdbc-iterable</artifactId>
           <version>1.0.3</version>
       </dependency>


   </dependencies>



</project>

资源的寻找地址在:http://mvnrepository.com/artifact/com.alexkasko.springjdbc/springjdbc-iterable/1.0.3


2.配置beans.xml,在resources下建立,如图。要记得路径file,xml,spring。如果没有spring选项,去找mawen资源库。

配置内容为:

<?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">

   <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
       <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
       <property name="username" value="root"></property>
       <property name="password" value="1234"></property>
   </bean>

   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <property name="dataSource" ref="dataSource"></property>
   </bean>
   <bean id="myJdbcTemplata" class="com.ptteng.MyJdbcTemplate">
   <property name="template" ref="jdbcTemplate"></property>
   </bean>

   <bean id="user" class="com.ptteng.User"></bean>

   </beans>

3.在java下建立com.ptteng包,建立MyJdbcTemplate.java类,在相同位置建立User.java类。


4.因为是新建的maven文件,所以要把以前JDBC连接数据库的java类迁移过来。把以前的表student2改为了user

package com.ptteng;

import java.sql.*;

public class JDBC {
public static void main(String[] args) {
Connection con;
       String driver = "com.mysql.jdbc.Driver";
       String url = "jdbc:mysql://localhost:3306/test";
       String user = "root";
       String password = "1234";
       try {
Class.forName(driver);
           con = DriverManager.getConnection(url, user, password);
           if (!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
           Statement statement = con.createStatement();
           String sql = "select * from user";
           ResultSet rs = statement.executeQuery(sql);
           System.out.println("--------");
           System.out.println("执行结果如下所示:");
           System.out.println("----------");
           System.out.println("ID"  + "  " +"曾用名");
           System.out.println("------------");
           String name = null;
           String test_qq = null;
           String gra_school = null;

           while (rs.next()) {
name = rs.getString("id");
               test_qq = rs.getString("username");

               System.out.println("id"  + " "  + "username");
           }
rs.close();
           con.close();
       } catch (ClassNotFoundException e) {
System.out.println("Sorry,can't find the Driver!");
           e.printStackTrace();
       } catch (SQLException e) {
e.printStackTrace();
       } catch (Exception e) {
e.printStackTrace();
       } finally {
System.out.println("数据库数据成功获取!!");

       }


}
}


5.最后关头,出错误了,没改过来。请庆东师兄看一下。就是在两处

ParameterizedBeanPropertyRowMapper有红线,中间的一处倒没有

如图:

package junit.Test;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.ptteng.MyJdbcTemplate;
import com.ptteng.User;

public class SpringTest {
   JdbcTemplate jTemplate=null;
   @Before
   public void before()
   {
       String path="com/ptteng/beans.xml";
       ApplicationContext application=new ClassPathXmlApplicationContext(path);
       MyJdbcTemplate template=(MyJdbcTemplate) application.getBean("myJdbcTemplate");
       jTemplate=template.getTemplate();
   }
@Test
   public void  TableTest()
   {
//  jTemplate.execute("create table user(user_id int primary key auto_increment, username varchar(30));");
       String sql="insert into t_user(username) value(?);";
       Object obj[]={"wh"};
       jTemplate.update(sql,obj);
   }
@Test
   public void updateTest()
   {
       String sql="update user set username=? where id=?";
       int hang=jTemplate.update(sql,new Object[]{"wh2",2});
       System.out.println(hang);
   }
@Test
   public void  queryTest()
   {
//      List<User>users=null;
//      String psc="select * from user";
//      users=jTemplate.query(psc, ParameterizedBeanPropertyRowMapper.newInstance(User.class));
//      for(User u:users)
//      {
//          System.out.println(u.getId()+":"+u.getUsername());
//      }
       String sql="select * from user where id="+1;
       System.out.println(jTemplate.queryForObject(sql, ParameterizedBeanPropertyRowMapper.newInstance(User.class)).getId());

   }
}


明天的计划:搞定JDBCTemplate。

遇到的问题:第五个。

收获:还是有收获的,对JDBCtemplate多了很多了解


java任务开始时间:2017.12.05

预计demo时间:2018.01-05

可能有延期风险,原因是:基础太差,很多任务的教程都卡壳,进行不下去。

禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html



返回列表 返回列表
评论

    分享到