时间:2021-05-20
本文主要介绍了SpringBoot Service和Dao的编写详解,分享给大家,具体如下:
效果图
配置环境
创建数据库
数据库中文编码
建表
create table `student` ( `id` int(11) Not NULL AUTO_INCREMENT COMMENT '主键自增id', `name` varchar(250) NOT NULL DEFAULT ' ' COMMENT '姓名', PRIMARY KEY(`id`))ENGINE=INNODB DEFAULT CHARSET=utf8;pom依赖和配置
mybatis 和 mysql
<!-- connect--><!-- 不同版本mybatis对应boot版本不同--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency><!-- mysql版本可以不指定--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>application.properties
# mysqlspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/rxguo_test?serverTimezone=UTC&characterEncoding=utf8&useSSL=falsespring.datasource.username=rootspring.datasource.password=java bean
package com.bennyrhys.com.shop.bean;public class Student { Integer id; String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; }}Controller
package com.bennyrhys.com.shop;import com.bennyrhys.com.shop.bean.Student;import com.bennyrhys.com.shop.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class StudentController { @Autowired StudentService studentService; @GetMapping("/student") public String getStudentById(@RequestParam Integer id) { Student student = studentService.getStudentById(id); return student.toString(); }}Service
package com.bennyrhys.com.shop.service;import com.bennyrhys.com.shop.bean.Student;import com.bennyrhys.com.shop.mapper.StudentMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class StudentService { @Autowired StudentMapper studentMapper; public Student getStudentById(Integer id) { return studentMapper.getStudentById(id); }}Mapper接口
package com.bennyrhys.com.shop.mapper;import com.bennyrhys.com.shop.bean.Student;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import org.springframework.stereotype.Repository;@Mapper@Repositorypublic interface StudentMapper { @Select("select * from student where id = #{id}") Student getStudentById(Integer id);}到此这篇关于SpringBoot Service和Dao的编写详解的文章就介绍到这了,更多相关SpringBoot Service和Dao编写内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
springBoot所有依赖和配置文件都写好的情况下1、dao接口的实现方法packagecom.cy.pj.sys.dao;importjava.util.L
一般maven多模块工程结构如下图,图中分为dao数据层和上层web层(当然还可以有service层),在进行多模块划分的时候,一般将dao层采用jar进行打包
AndroidIntentService详解一、IntentService简介IntentService是Service的子类,比普通的Service增加了额外
spring@component的作用详细介绍1、@controller控制器(注入服务)2、@service服务(注入dao)3、@repositorydao
环境配置1>先创建maven的quickstart项目;并且创建dao层,service层,controller层,po层,mapper,resources以及