发表于: 2019-10-19 22:55:22

1 944


今天完成的事情:

jdbctemplate连接数据库


文件结构


1.  applicationContext.xml文件       用的spring自带的数据源

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="username" value="root"></property>
       <property name="password" value="451976"></property>
       <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
       <property name="Url" value="jdbc:mysql://localhost:3306/student?serverTimezone=UTC"></property>
   </bean>

   <!--设置一个名为jdbcTemplatebean,   引入(refdataSource-->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <property name="dataSource" ref="dataSource"></property>
   </bean>
</beans>


2.  实体类文件   student

package springenity;

public class Student {
private int id;
   private String name;
   private int qq;
   private String type;
   private String time;
   private int stunum;
   private String daily;
   private String wish;
   private String senior;

   public int getId() {
return id;
   }
public void setId(){
this.id=id;
   }
public String getName(){
return name;
   }
public void setName(){
this.name=name;
   }
public int getqq(){
return qq;
   }
public void setqq() {
this.qq=qq;
   }
public String getType(){
return type;
   }
public void setType(){
this.type=type;
   }
public String getTime() {
return time;
   }
public void settime(){ this.time=time; }
public int getstunum() {
return stunum;
   }
public void setstunum(){ this.stunum=stunum; }
public String getdaily(){
return daily;
   }
public void setdaily(){
this.daily=daily;
   }
public String getWish() {
return wish;
   }
public void setWish() {
this.wish=wish;
   }
public String getsenior(){
return senior;
   }
public void setsenior(){
this.senior=senior;
   }

//String to string     toString返回此对象本身(它已经是一个字符串)

   @Override
   public String toString() {
return "Person [id=" + id + ", name=" + name + ", qq=" + qq + ", type=" + type + ", time" + time + ", time" + stunum + ", time" + daily + ", time" + wish + ", time" + senior +"]";
   }
}

3. 测试一下把   studentTest

public class studentTest {

private ApplicationContext ctx = null;
   private JdbcTemplate jdbcTemplate = null;
   {
//加载applicationcontext 配置文件
       ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");

       //读取bean容器  jdbctemplate      连接数据库
       jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
   }
@Test
   public void testUpdate() {
String sql = "delete from bj where id = ? ";
       jdbcTemplate.update(sql,  1);
       System.out.println("删除成功!");
   }
}


4. studentDao接口

package springdao;
   public interface StudentDao{
       void count();
       void select();
       void delete();
       void insert();
       void update();
}

里面有5个方法    第1个统计表单的总数量  剩下4个增删查改


5.studentDaoImpl     实现接口

package springdao;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.JdbcTemplate;

import springenity.Student;

import java.util.List;


//实现studentDao接口
public class StudentDaoImpl implements StudentDao {

@Test
   public void count() {
//读取xml文件   连接数据库
       ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
       JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");

       //调用jdbcTemplate方法得到记录
       String sql = "select count(*) from bj";

       /*queryForObject(String sql,Class<T> requiredType)
        *第一个参数:sql语句

        * 第二个参数:返回类型的class          

        */


        //对这段代码不太理解  

       int count = jdbcTemplate.queryForObject(sql, Integer.class);

       System.out.println(count);
   }

(1) 统计表单数量  


(2)查找 insert    

这个还没实现

因为查询后输出显示的方法,我使用的  new myRowMapper方法

 @Test
   public void select() {
//读取xml文件   连接数据库
   ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
   JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");

   String sql = "select * from bj";


    /*
   query(String sql,RowMapper<T> rowMapper,Object ... args)
   第一个参数:SQL语句
   第二个参数:RowMapper接口,自己写类实现数据封装
   第三个参数:可变参数
    */

    //上面写了后面的含义,但我没看懂
   List<Student> list = jdbcTemplate.query(sql,new MyRowMapper());
   System.out.println(list);
   System.out.println("查询成功!!!");
}
我需要新建MyRowMapper类实现RowMapper接口,重写mapRow方法,指定返回User对象

呃...百度搜了下 类实现myrowmapper接口    相关文章看的有点懵


看别的师兄日报还有一种方法可以查询

下图是   师兄的日报

list<student> finALL    这个需要list<>相关知识   不懂   也得搜索学习

明天把这个解决了


(3)删除 delete

public void delete() {

//读取xml文件   连接数据库
   ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
   JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");

   String sql = "delete from bj where id =  ? ";

   //输出
   int pp = jdbcTemplate.update(sql, 8);
   System.out.println("删除成功!");
}

(4)插入

@Test
public void insert() {

//读取xml文件   连接数据库
   ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
   JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");

   String sql = "insert into bj (id,name,qq,type,time,stunum,daily,wish,senior)values(?,?,?,?,?,?,?,?,?)";
   //输出
   jdbcTemplate.update(sql, 12, "刘民", 785482221, "前端", "63", 5484, "5588/daily", "不学习就胖10", "王文");
   System.out.println("添加成功!");
}


(5)修改 update

public void update() {

//读取xml文件   连接数据库
       ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
       JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");

       String sql = "update bj set name=? where id = ?";
       //输出
       jdbcTemplate.update(sql, "我改了,有什么好说的", 4);
       System.out.println("更改成功!");
   }
}



明天计划的事情:

把jdbctemplate    查询  的问题解决

然后再看看别人搭建的jdbctemplate   看有哪些和我不一样的


进展顺利 就看mybatis   不想在这个jdbctemplate再浪费时间了


遇到的问题:


自己测试时,碰到很多报错

百度解决完一个还有一个...再解决还有一个,很多

有自己的原因,也有别的原因

浪费了很多时间  


记录了一个奇怪的报错,居然是时区差异引起的


改完就好啦



收获:


今天通过写jdbctemplate    明白一件事

写代码才是解决问题的好方式      

能在写的过程中发现很多具体的问题

光看理论真的会把人看乱


还有  师兄日报是个好东西



返回列表 返回列表
评论

    分享到