package spring.jdbc.entity;
public class student {
//属性与数据库的student表的列名一样
String Sno;
String Sname;
String Sex;
String Sbrithday;
String Sclass;
//设置了有参构造方法,系统的默认的无参构造方法被覆盖,需自己手动建立一个无参构造方法以便在其他地方实例化对象
public student() {}
//设置有参构造方法,以便test中直接给对象赋值
public student(String a,String b,String c,String d,String e) {
Sno=a;
Sname=b;
Sex=c;
Sbrithday=d;
Sclass=e;
}
public String getSno() {
return Sno;
}
public void setSno(String sno) {
Sno = sno;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public String getSbrithday() {
return Sbrithday;
}
public void setSbrithday(String sbrithday) {
Sbrithday = sbrithday;
}
public String getSclass() {
return Sclass;
}
public void setSclass(String sclass) {
Sclass = sclass;
}
public void tostring() {
System.out.println(Sno+" "+Sname+" "+Sex+" "+Sbrithday+" "+Sclass);
}
}
package spring.jdbc.dao;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.mysql.cj.result.StringValueFactory;
import spring.jdbc.entity.student;
@Repository("stuDao")
public class stuDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public int addstudent(student s) {
String sql="insert into student(Sno,Sname,Sex,Sbirthday,Sclass) values(?,?,?,?,?)";
//将参数s的键值存到数组对象中
Object[] obj=new Object[] {
s.getSno(),
s.getSname(),
s.getSex(),
s.getSbrithday(),
s.getSclass(),
};
//执行添加操作,update方法返回的是操作数据库后受影响的行数
int num=this.jdbcTemplate.update(sql, obj);
return num;
}
public int updateNamebySno(student s) {
String sql="update student set Sname=? where Sno=?";
Object[] obj =new Object[] {
s.getSname(),
s.getSno(),
};
int num =this.jdbcTemplate.update(sql, obj);
return num;
}
public int deletestudentbySno(String sno) {
String sql="delete from student where Sno=?";
int num =this.jdbcTemplate.update(sql, sno);
return num;
}
public List<student> findAllstudent(){
String sql="select * from student";
RowMapper<student> rowMapper=new BeanPropertyRowMapper<student>(student.class);
return this.jdbcTemplate.query(sql, rowMapper);
}
public String findNameBySno(String sno) {
String sql="select Sname from student where Sno=?";
String name=this.jdbcTemplate.queryForObject(sql, new Object[] {sno}, String.class);
return name;
}
public int findNumbers() {
String sql="select COUNT(*) from student";
int total =this.jdbcTemplate.queryForObject(sql, Integer.class);
return total;
}
public List<Map<String, Object>> findScBySno(String sno){
String sql="select sc.Sno,Sname,Cname,grade from student,sc,course where student.sno=sc.sno and course.cno=sc.cno"+" and sc.sno=?";
return this.jdbcTemplate.queryForList(sql, new Object[] {sno});
}
}
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 扫描包,使注解生效 -->
<context:component-scan base-package="spring.jdbc"></context:component-scan>
</beans>
package spring.jdbc.test;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import spring.jdbc.dao.stuDao;
import spring.jdbc.entity.student;
public class test {
public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
student s=new student("027","猪猪侠","女","2001年2月16日","19软件本科3班");
student s2=new student("027","猪猪猪侠","女","2001年2月16日","19软件本科3班");
stuDao sd=(stuDao) context.getBean("stuDao");
// System.out.println("添加学生信息受影响行数:"+sd.addstudent(s));
// System.out.println("修改学生信息受影响行数:"+sd.updateNamebySno(s2));
// System.out.println("删除学生信息受影响行数:"+sd.deletestudentbySno("001"));
// System.out.println("查询所有学生记录");
// List list=sd.findAllstudent();
// for(student st:list) st.tostring();
// System.out.println("查询025学生记录="+sd.findNameBySno("025"));
// System.out.println("学生人数="+sd.findNumbers());
List<Map<String, Object>> list2=sd.findScBySno("005");
for(String k:list2.get(0).keySet()) System.out.print(k+" ");
System.out.println();
for(Map<String, Object> m:list2) {
for(String k:m.keySet()) System.out.print(m.get(k)+" ");
System.out.println();
}
}
}
源代码下载点击此处