培训网站免费在线做ppt
培训网站免费,在线做ppt,wordpress横幅,在线美图秀秀在线制作文章目录一、创建SpringBoot项目二、添加Mybatis相关依赖三、数据源配置四、创建事务的模型实体类五、创建和数据库交互联系的映射关系类六、创建业务接口和实现类七、创建控制器类八、请求验证一、创建SpringBoot项目
如何创建详见#xff1a;IDEA 创建 SpringBoot 项目 二、…
文章目录一、创建SpringBoot项目二、添加Mybatis相关依赖三、数据源配置四、创建事务的模型实体类五、创建和数据库交互联系的映射关系类六、创建业务接口和实现类七、创建控制器类八、请求验证一、创建SpringBoot项目
如何创建详见IDEA 创建 SpringBoot 项目 二、添加Mybatis相关依赖
以前开发Web项目我们都知道要想把数据添加到数据库不仅必须要数据库的驱动程序还要有各种各样的配置文件像java Bean配置数据源配置对象和数据库字段的映射配置等等。使用SpringBoot开发我们只需要加入依赖文件就可以了SpringBoot已经都帮我配置好了。配置如下图所示
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope
/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.1/version
/dependency三、数据源配置
在application.properties中配置数据库连接的相关信息
spring.datasource.driver-class-namecom.mysql.jdbc.Driver
spring.datasource.urljdbc:mysql://127.0.0.1:18103/db_test?characterEncodingGBK
spring.datasource.usernameroot
spring.datasource.passwordroot四、创建事务的模型实体类
编程是利用面向对象的思想把自然界中的事物抽象成模型利用模型来解决实际中的问题。如下图
package com.springboottest.bean;public class StudentBean {private int id;private String name;public StudentBean() {}public int getId() {return id;}public void setId(int id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}
}注这里的字段名称与数据库表字段名称一致。 五、创建和数据库交互联系的映射关系类
这个类主要是和数据进行交互联系的需要配置好实体类和数据库字段的映射关系。由于SpringBoot已经做了大量的工作我们只需要做好相关注解就可以使用了。如下图所示
package com.springboottest.sql.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;Mapper
public interface StudentMapper {Select(select * from tb_student where name#{name})StudentBean getStudentInfoByName(String name);
}Mapper 表明该类是一个Mapper接口使用Select、Insert等注解我们可以直接在类中书写sql语句来实现我们的目的。 六、创建业务接口和实现类
我们在接口类里定义要实现的业务功能接口在它的实现类里实现接口。接口类如下图
package com.springboottest.sql.service;import com.springboottest.bean.StudentBean;public interface StudentService {StudentBean getStudentInfoByName(String name);
}实现类如下图
package com.springboottest.sql.service;import com.springboottest.bean.StudentBean;
import com.springboottest.sql.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;Service
public class StudentServiceImpl implements StudentService{Autowiredprivate StudentMapper studentMapper;OverrideTransactionalpublic StudentBean getStudentInfoByName(String name) {return studentMapper.getStudentInfoByName(name);}
}Service注解表明它是一个服务类Bean可以被SpringBoot识别使用相当于以前在xml里配置的bean。 七、创建控制器类
Web项目的请求经过映射找到控制器类里对应的方法然后再实现完业务返回响应信息。如下图
package com.springboottest.controller;import com.springboottest.bean.StudentBean;
import com.springboottest.sql.MySQLProcessor;
import com.springboottest.sql.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/mysql)
public class SqlController {Autowiredprivate StudentService studentService;RequestMapping(value /student)public String studentSelect(RequestParam String name){StudentBean bean studentService.getStudentInfoByName(name);if(bean ! null){return Name bean.getName();} else {return null;}}
}八、请求验证
请求地址http://localhost:8991/mysql/student?nametom
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/91949.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!