1.该项目启动所需jia包
2.该项目的结构
3.项目启动所需的配置文件application.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> 6 7 8 <!-- 1配置数据源 ,数据库为jdbc--> 9 <bean id="dataSource" class= 10 "org.springframework.jdbc.datasource.DriverManagerDataSource"> 11 <!--数据库驱动 --> 12 <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> 13 <!--连接数据库的url --> 14 <property name="url" value="jdbc:mysql://localhost:3306/jdbc?useSSL=false&serverTimezone=UTC" /> 15 <!--连接数据库的用户名 --> 16 <property name="username" value="root" /> 17 <!--连接数据库的密码 --> 18 <property name="password" value="password" /> 19 </bean> 20 <!-- 2配置JDBC模板 --> 21 <bean id="jdbcTemplate" 22 class="org.springframework.jdbc.core.JdbcTemplate"> 23 <!-- 默认必须使用数据源 --> 24 <property name="dataSource" ref="dataSource" /> 25 </bean> 26 27 <!--定义id为student的Bean--> 28 <bean id="studentDao01" class="cn.edu.hnzj.dao.impl.T_StudentDaoImpl"> 29 <!-- 将jdbcTemplate注入到accountDao实例中 --> 30 <property name="jdbcTemplate" ref="jdbcTemplate" /> 31 </bean> 32 33 <!-- 1配置数据源,数据库为mybatis --> 34 <bean id="dataSource02" class= 35 "org.springframework.jdbc.datasource.DriverManagerDataSource"> 36 <!--数据库驱动 --> 37 <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> 38 <!--连接数据库的url --> 39 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC" /> 40 <!--连接数据库的用户名 --> 41 <property name="username" value="root" /> 42 <!--连接数据库的密码 --> 43 <property name="password" value="password" /> 44 </bean> 45 <!-- 2配置JDBC模板 --> 46 <bean id="jdbcTemplate01" 47 class="org.springframework.jdbc.core.JdbcTemplate"> 48 <!-- 默认必须使用数据源 --> 49 <property name="dataSource" ref="dataSource02" /> 50 </bean> 51 52 <!--定义id为T_Student的Bean--> 53 <bean id="studentDao02" class="cn.edu.hnzj.dao.impl.StudentDaoImpl"> 54 <!-- 将jdbcTemplate注入到accountDao实例中 --> 55 <property name="jdbcTemplate01" ref="jdbcTemplate01" /> 56 </bean> 57 </beans>
4.1在cn.edu.hnzj.po包下的Student.java文件
1 package cn.edu.hnzj.po; 2 /** 3 * 持久化mybatis数据库的t_student 4 */ 5 public class Student { 6 private Integer id;//主键id 7 private String username;//用户名 8 private String password;//密码 9 private String email;//邮箱 10 11 public Integer getId() { 12 return id; 13 } 14 public void setId(Integer id) { 15 this.id = id; 16 } 17 public String getUsername() { 18 return username; 19 } 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 public String getPassword() { 24 return password; 25 } 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 public String getEmail() { 30 return email; 31 } 32 public void setEmail(String email) { 33 this.email = email; 34 } 35 @Override 36 public String toString() { 37 return "T_Student [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]"; 38 } 39 40 }
4.2在cn.edu.hnzj.po包下的T_Student.java文件
1 package cn.edu.hnzj.po; 2 /** 3 * 持久化jdbc数据库的t_student 4 */ 5 public class T_Student { 6 private Integer id;//主键id 7 private String username;//用户名 8 private String password;//密码 9 private String email;//邮箱 10 11 public Integer getId() { 12 return id; 13 } 14 public void setId(Integer id) { 15 this.id = id; 16 } 17 public String getUsername() { 18 return username; 19 } 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 public String getPassword() { 24 return password; 25 } 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 public String getEmail() { 30 return email; 31 } 32 public void setEmail(String email) { 33 this.email = email; 34 } 35 @Override 36 public String toString() { 37 return "T_Student [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]"; 38 } 39 40 41 }
5.1在cn.edu.hnzj.dao包下的StudentDao.java文件
1 package cn.edu.hnzj.dao; 2 3 import cn.edu.hnzj.po.T_Student; 4 5 public interface StudentDao { 6 7 /* 8 * 往mybatis库student表中批量添加数据 9 */ 10 11 public int insertAll(T_Student Student); 12 }
5.2在cn.edu.hnzj.dao包下的T_StudentDao.java文件
1 package cn.edu.hnzj.dao; 2 3 import java.util.List; 4 5 import cn.edu.hnzj.po.T_Student; 6 7 8 public interface T_StudentDao { 9 10 /** 11 * 查询jdbc库中t_student表的所有数据 12 */ 13 public List<T_Student> selectAll(); 14 15 }
6.1 在cn.edu.hnzj.dao.impl包下的StudentDaoImpl.java文件
1 package cn.edu.hnzj.dao.impl; 2 3 import org.springframework.jdbc.core.JdbcTemplate; 4 5 import cn.edu.hnzj.dao.StudentDao; 6 import cn.edu.hnzj.po.T_Student; 7 8 public class StudentDaoImpl implements StudentDao { 9 //声明jdbcTemplate01属性 10 private JdbcTemplate jdbcTemplate01; 11 //添加jdbcTemplate01的set方法,用于实现依赖注入 12 public void setJdbcTemplate01(JdbcTemplate jdbcTemplate01) { 13 this.jdbcTemplate01 = jdbcTemplate01; 14 } 15 16 /* 17 * 往mybatis库student表中批量添加数据 18 */ 19 @Override 20 public int insertAll(T_Student student) { 21 //定义sql语句 22 String sql = "insert into student values(?,?,?,?)"; 23 //定义数组来存储sql语句中的参数 24 Object[] obj = new Object[] { 25 student.getId(), 26 student.getUsername(), 27 student.getPassword(), 28 student.getEmail() 29 }; 30 //执行添加操作,返回的是受sql语句影响的记录条数 31 int row = this.jdbcTemplate01.update(sql, obj); 32 return row; 33 } 34 35 36 }
6.2在cn.edu.hnzj.dao.impl包下的T_StudentDaoImpl.java.java文件
1 package cn.edu.hnzj.dao.impl; 2 3 import java.util.List; 4 5 import org.springframework.jdbc.core.BeanPropertyRowMapper; 6 import org.springframework.jdbc.core.JdbcTemplate; 7 import org.springframework.jdbc.core.RowMapper; 8 9 import cn.edu.hnzj.dao.T_StudentDao; 10 import cn.edu.hnzj.po.T_Student; 11 12 public class T_StudentDaoImpl implements T_StudentDao { 13 //声明jdbcTemplate属性 14 private JdbcTemplate jdbcTemplate; 15 //添加jdbcTemplate的set方法,用于实现依赖注入 16 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 17 this.jdbcTemplate = jdbcTemplate; 18 } 19 20 @Override 21 public List<T_Student> selectAll() { 22 //定义sql语句 23 String sql = "select * from t_student"; 24 //创建一个BeanPropertyRowMapper对象 25 RowMapper<T_Student> rowMapper = new BeanPropertyRowMapper<T_Student>(T_Student.class); 26 27 //调用query方法执行查询操作 28 return this.jdbcTemplate.query(sql, rowMapper); 29 } 30 31 }
7在cn.edu.hnzj.test包下的TestStudent.java文件
1 package cn.edu.hnzj.test; 2 3 import java.util.List; 4 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 import cn.edu.hnzj.dao.StudentDao; 10 import cn.edu.hnzj.dao.T_StudentDao; 11 import cn.edu.hnzj.po.T_Student; 12 13 14 15 public class TestStudent { 16 /** 17 * 把jdbc数据库的t_student表的所有数据 18 * 都添加到mybatis数据库的student表中。 19 */ 20 @Test 21 public void Test01() { 22 //初始化Spring容器,加载配置文件applicationContext.xml和applicationContext.xml 23 ApplicationContext application01 = new ClassPathXmlApplicationContext("applicationContext.xml"); 24 ApplicationContext application02 = new ClassPathXmlApplicationContext("applicationContext.xml"); 25 //通过容器获取t_StudentDao和studentDao 26 T_StudentDao t_StudentDao = (T_StudentDao) application01.getBean("studentDao01"); 27 StudentDao studentDao = (StudentDao) application02.getBean("studentDao02"); 28 //调用接口对应的方法 29 T_Student t_student = new T_Student(); 30 List<T_Student> list = t_StudentDao.selectAll(); 31 int row = 0; 32 //查询出的结果,添加到另一张表中 33 for(int i=0; i<list.size(); i++) { 34 t_student.setId(list.get(i).getId()); 35 t_student.setUsername(list.get(i).getUsername()); 36 t_student.setPassword(list.get(i).getPassword()); 37 t_student.setEmail(list.get(i).getEmail()); 38 39 row = studentDao.insertAll(t_student); 40 } 41 42 if(row >0) { 43 System.out.println(true); 44 }else { 45 System.out.println(false); 46 } 47 } 48 49 }
8.最终的运行结果的截图