SpringBoot链接数据库的3种形式

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 selectStudenet();
    public Student selectStudenetByNum(Integer stuNum);
}

编写业务类继承JpaRepository类,@Query中就是我们所要执行的SQL语句。

public interface StudentRepository extends JpaRepository {
    @Query(nativeQuery = true, value =
            "select * from student")
    public List selectStudent();
}

编写UserDao的实现类重写UserDao中的方法。

public class StudentDaoImpl implements StudentDao {

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public List selectStudenet() {
        return studentRepository.selectStudent();
    }
}

查看测试方法

 @Test
    public void contextLoads() {
        try{
            List studentList = studentDaoImpl .selectStudenet();
            for(Student student : studentList){
                System.out.println(student.toString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        
    }

查看运行结果


image.png

第二种方式JdbcTemplate

public interface StudentDao {
   public List 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 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());
    }

运行结果


image.png

第三种整合MyBatis

image.png

扫描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文件





 
    
        
        
        
        
    
 
    
 


运行结果


image.png

你可能感兴趣的:(SpringBoot链接数据库的3种形式)