student数据表。
+--------+----------+------------+-------------+
| stu_id | stu_name | stu_number | stu_phone |
+--------+----------+------------+-------------+
| 1 | 张三 | 1234567890 | 18392601234 |
| 2 | 李四 | 1234567891 | 18392601235 |
+--------+----------+------------+-------------+
student实体类
@Table(name = "users")
@Entity
public class Student {
@Id
@GeneratedValue
@Column(name="stu_id")
private Integer stuId;
@Column(name="stu_name")
private String stuName;
@Column(name="stu_number")
private Integer stuNumber;
@Column(name="stu_phone")
private String stuPhone;
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Integer getStuNumber() {
return stuNumber;
}
public void setStuNumber(Integer stuNumber) {
this.stuNumber = stuNumber;
}
public String getStuPhone() {
return stuPhone;
}
public void setStuPhone(String stuPhone) {
this.stuPhone = stuPhone;
}
@Override
public String toString() {
return "Student{" +
"stuName='" + stuName + '\'' +
", stuNumber=" + stuNumber +
", stuPhone='" + stuPhone + '\'' +
'}';
}
}
第一种方式JPAl
public interface StudentDao {
public List<Student> selectStudenet();
public Student selectStudenetByNum(Integer stuNum);
}
编写业务类继承JpaRepository类,@Query中就是我们所要执行的SQL语句。
public interface StudentRepository extends JpaRepository<Student, Integer> {
@Query(nativeQuery = true, value =
"select * from student")
public List<Student> selectStudent();
}
编写UserDao的实现类重写UserDao中的方法。
public class StudentDaoImpl implements StudentDao {
@Autowired
private StudentRepository studentRepository;
@Override
public List<Student> selectStudenet() {
return studentRepository.selectStudent();
}
}
查看测试方法
@Test
public void contextLoads() {
try{
List<Student> studentList = studentDaoImpl .selectStudenet();
for(Student student : studentList){
System.out.println(student.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}
查看运行结果
第二种方式JdbcTemplate
public interface StudentDao {
public List<Student> selectStudenet();
public Student selectStudenetByNum(Integer stuNum);
}
编写Dao实现类
public class studentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Student> selectStudenet() {
return null;
}
@Override
public Student selectStudenetByNum(Integer stuNum) {
String sql = "select stu_name ,stu_phone ,stu_number from student where stu_number=?";
Student student = new Student();
jdbcTemplate.query(sql, new Object[]{stuNum}, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
student.setStuNumber(stuNum);
student.setStuName(rs.getString("stu_name"));
student.setStuPhone(rs.getString("stu_phone"));
}
});
return student;
}
}
编写studentService
@Service
public class StudentService implements StudentDao {
@Autowired
private studentDaoImpl studentDao;
@Override
public Student selectStudenetByNum(Integer stuNum) {
Student student = null;
try{
student = studentDao.selectStudenetByNum(stuNum);
}catch (Exception e){
e.printStackTrace();
}
return student;
}
}
测试方法
@Test
public void contextLoad2() {
Student student = studentService.selectStudenetByNum(1234567890);
System.out.println(student.toString());
}
运行结果
第三种整合MyBatis
扫描Dao接口
@SpringBootApplication
@MapperScan("com.cn.boothouse.dao")
public class BoothouseApplication {
public static void main(String[] args) {
SpringApplication.run(BoothouseApplication.class, args);
}
}
@Repository
public interface StudentMapper {
publci Student search(Integer stuNum);
}
mapper文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.cn.boothouse.entity.student">
<result column="stu_id" jdbcType="Integer" property="stuId" />
<result column="stu_name" jdbcType="varchar" property="stuName" />
<result column="stu_number" jdbcType="Integer" property="stuNumber" />
<result column="stu_phone" jdbcType="varchar" property="stuPhone" />
</resultMap>
<select id="search" resultType="com.cn.boothouse.entity.student">
select * from student where stu_id= #{stuNum}
</select>
</mapper>
运行结果