3,保证能连接网络上网,以便后续能下载所需的JAR包;
二,工程样本建立
浏览器登录 https://start.spring.io/ 构建一个简单的MVC功能工程,增加的Dependencies依赖:web, Rest Repositories, Thymeleaf,jdbc,sql server,mybatis 然后点击Generate Project 产生并下载项目工程样例的打包文件,如下图,
三,工程导入Eclipse开发环境,下载相关依赖JAR包,
1,项目下载后解压到本地某个地方,文件夹可以按自己要求重命名,也可保留原样,然后:
Eclipse --》Import --》弹出窗口Maven --》Existing Maven Projects --》Next --》弹出窗口Browse... 选择工程所在文件夹 --》Finish确认,完成项目导入Eclipse;
2,导入工程后,可根据需要修改工程名称,例如我这里直接右键单击工程名称,Refactor --》Rename,工程名改为springBoot01Database;然后下载相关jar包,Eclipse --》Project --》clean... 清空缓存目的是触发下载pom.xml文件中的指定依赖的JAR包文件,这个确保能连接上网,快慢取决于网速,一般几分钟到半小时左右可完成;
结果整个工程图如下,
四,工程 pom.xml 修改,内容如下,
4.0.0
com.qyh
springBoot01Database
0.0.1-SNAPSHOT
jar
springBoot01Database
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-rest
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.microsoft.sqlserver
mssql-jdbc
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true
test
compile
springBoot01Database
org.springframework.boot
spring-boot-maven-plugin
true
对应库表结构
create table member (
recID bigint identify(1,1) primiry key,
memberId varchar(10) null,
memberName varchar(50) ,
sex varchar(1)
)
五,配置文件 application.properties 文件修改,
#===================================================
#application-dev.properties
#===================================================
# server evn setting
#===================================================
server.address=127.0.0.1
server.port=8080
server.servlet.contextPath=/
server.session-timeout=30
#=====For MVC setting=====
spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
#===================================================
# database setting
#===================================================
#=====For connect ms sql server use jdbc pool=====
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=myDBName
spring.datasource.username=DBUserName
spring.datasource.password=DBPassword
#=====For connect mysql use jdbc pool=====
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/myDBName?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
#spring.datasource.username=DBUserName
#spring.datasource.password=DBPassword
#=====For connect oracle db use jdbc pool=====
#spring.datasource.driverClassName=oracle.jdbc.OracleDriver
#spring.datasource.url=jdbc:oracle:thin:@localhost:1521:oracleSID
#spring.datasource.username=DBUserName
#spring.datasource.password=DBPassword
#=====For common X DB connect setting=====
spring.datasource.initialSize=10
spring.datasource.minIdle=10
spring.datasource.maxActive=50
spring.datasource.maxWait=60000
# more setting add here...
#=====For mybatis setting======
mybatis.typeAliasesPackage=com.qyh.pro01.model,com.qyh.pro01.param
#mybatis.configLocations=classpath:mybatis/mybatis-config.xml #no active now
mybatis.mapperLocations=classpath:mapper/*.xml
#===================================================
# other setting
#===================================================
#===================================================
#application.properties
#===================================================
spring.profiles.active=dev
#active dev evn
六,启动类中相关功能注解使用
package com.qyh.springBootApp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
/**
* springboot+mybatis+jdbc+mssql实例
* @author shenzhenNBA
* @since 2018.06.09
*/
@SpringBootApplication
@EntityScan("com.qyh.pro01.model,com.qyh.pro01.param")
@MapperScan("com.qyh.pro01.dao")
@ComponentScan("com.qyh.pro01.common"
+ ",com.qyh.pro01.controller"
+ ",com.qyh.pro01.service.impl")
public class SpringBoot01DBApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01DBApplication.class, args);
}
}
七,工程相关的DAO接口,对应库表mapper映射的XML文件,实体model和查询参数param,业务接口,业务接口实现等等编写各个相应类或XML代码如下,
1,DAO接口类,
package com.qyh.pro01.dao;
/**
* @author shenzhenNBA
* @since 2018.06.09
*/
public abstract interface BaseDaoService {
public T getByRecId(ID recId);
public int insert(T t) throws Exception;
public int save(T t) throws Exception;
public int update(T t) throws Exception;
public int delete(T t) throws Exception;
public int deleteByRecId(ID recId) throws Exception;
}
package com.qyh.pro01.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
/**
* @author shenzhenNBA
* @since 2018.06.09
*/
@Repository
public interface MemberService extends BaseDaoService {
public T getMemberByMemberId(String memberId);
public List queryAllMember();
}
2,实体类代码
package com.qyh.pro01.model;
/**
* @author shenzhenNBA
* @since 2018.06.09
*/
public class Member implements java.io.Serializable {
private static final long serialVersionUID=1000000000010L;
private Long recId;
private String memberId;
private String memberName;
private String sex;
public Member() {}
public Member(String memberId, String memberName) {
this.memberId = memberId;
this.memberName = memberName;
}
public Long getRecId() {
return this.recId;
}
public void setRecId(Long recId) {
this.recId = recId;
}
public String getMemberId() {
return this.memberId;
}
public void setMemberId(String memberId) {
this.memberId = memberId;
}
public String getMemberName() {
return this.memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package com.qyh.pro01.param;
/**
* @author shenzhenNBA
* @since 2018.06.09
*/
public class BaseParam implements java.io.Serializable {
private static final long serialVersionUID = 201608245000L;
private Long previousPage;
private Long nextPage;
private Long totalRecord;
private Long totalPage;
private Long offsetRecord;
private Long rows;
private Long page;
private String sort;
private String order;
public BaseParam() {
this.previousPage = 1L;
this.nextPage = 1L;
this.totalRecord = 1L;
this.totalPage = 1L;
this.rows = 10L;
this.page = 1L;
this.sort = "";
this.order = "";
}
public Long getPreviousPage() {
previousPage = page - 1;
if(previousPage < 1){
previousPage = 1L;
}
return previousPage;
}
public void setPreviousPage(Long previousPage) {
this.previousPage = previousPage;
}
public Long getNextPage() {
if(nextPage > totalPage){
nextPage = totalPage;
}
return nextPage;
}
public void setNextPage(Long nextPage) {
this.nextPage = nextPage;
}
public Long getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(Long totalRecord) {
if(totalRecord < 0){
totalRecord = 0L;
}
this.totalRecord = totalRecord;
}
public Long getTotalPage() {
if(totalRecord != null && rows != null){
totalPage = (totalRecord % rows == 0) ? (totalRecord / rows) : (totalRecord / rows + 1);
}else{
totalPage = 1L;
}
return totalPage;
}
public void setTotalPage(Long totalPage) {
this.totalPage = totalPage;
}
public Long getRows() {
if(rows < 1){
rows = 1L;
}
return rows;
}
public void setRows(Long rows) {
this.rows = rows;
}
public Long getPage() {
if(page < 1){
page = 1L;
}
return page;
}
public void setPage(Long page) {
this.page = page;
}
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public Long getOffsetRecord() {
if(page != null && rows != null){
if(page < 1){
page = 1L;
}
if(page > getTotalPage()){
page = getTotalPage();
}
offsetRecord = (page - 1) * rows ;
}else{
offsetRecord = 0L;
}
return offsetRecord;
}
public void setOffsetRecord(Long offsetRecord) {
this.offsetRecord = offsetRecord;
}
}
package com.qyh.pro01.param;
/**
* @author shenzhenNBA
* @since 2018.06.09
*/
public class MemberParam extends BaseParam {
private static final long serialVersionUID = 3748682284490848846L;
private String memberId;
private String memberName;
private String sex;
public MemberParam() {}
public String getMemberId() {
return memberId;
}
public void setMemberId(String memberId) {
this.memberId = memberId;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
3,业务类接口代码
package com.qyh.pro01.service;
/**
* @author shenzhenNBA
* @since 2018.06.09
* 业务类基本接口类
*/
public interface BaseBizService extends java.io.Serializable {
public T getByRecId(ID recId);
public int insert(T t) throws Exception;
public int save(T t) throws Exception;
public int update(T t) throws Exception;
public int delete(T t) throws Exception;
public int deleteByRecId(ID recId) throws Exception;
}
package com.qyh.pro01.service;
import java.util.List;
/**
* @author shenzhenNBA
* @since 2018.06.09
* 业务接口类
*/
public interface BizMemberService extends BaseBizService {
public T getMemberByMemberId(String memberId);
public List queryAllMember();
}
4,业务接口实现类代码,
package com.qyh.pro01.service.impl;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qyh.pro01.dao.MemberService;
import com.qyh.pro01.model.Member;
import com.qyh.pro01.param.MemberParam;
import com.qyh.pro01.service.BizMemberService;
/**
* @author shenzhenNBA
* @since 2018.06.09
* 业务接口实现类
*/
@Service("bizMemberService")
public class BizMemberServiceImpl implements BizMemberService {
private static Log LOG = LogFactory.getLog(BizMemberServiceImpl.class);
private static final long serialVersionUID = 20170826010130L;
@Autowired
private MemberService memberDaoService;
@Override
public Member getByRecId(Long recId) {
return memberDaoService.getMemberByRecId(recId);
}
@Override
public int insert(Member t) throws Exception {
return memberDaoService.insert(t);
}
@Override
public int update(Member t) throws Exception {
return memberDaoService.update(t);
}
@Override
public int delete(Member t) throws Exception {
return memberDaoService.delete(t);
}
@Override
public int deleteByRecId(Long recId) throws Exception {
return memberDaoService.deleteByRecId(recId);
}
@Override
public Member getMemberByMemberId(String memberId) {
return memberDaoService.getMemberByMemberId(memberId);
}
@Override
public List queryAllMember() {
return memberDaoService.queryAllMember();
}
}
5,库表映射mapper的XML文件代码,
SELECT top 1 recID from [member]
where memberName = #{memberName}
order by recID desc
update [member] set
memberId = #{memberId},
memberName = #{memberName},
sex = #{sex}
where recID = #{recId}
delete from [member] where recID = #{recId}
delete from [member] where recID = #{recId}
delete from [member] where memberID = #{memberId}
八,controller中使用业务service,package com.qyh.pro01.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qyh.pro01.model.Member;
import com.qyh.pro01.service.BizMemberService;
/**
* @author shenzhenNBA
* @since 2018.06.09
*/
@Controller
@RequestMapping("/member")
public class MemberController {
@Autowired
BizMemberService memberService;
@RequestMapping("")
public String memberIndex (
HttpServletRequest request
,HttpServletResponse response
,HttpSession session
,Model model) {
model.addAttribute("userName", "zhangsan");
Member member = memberService.getMemberByMemberId("1000");
model.addAttribute("member", member);
return "/view/member.html";
}
@RequestMapping("/list")
public String memberList (
HttpServletRequest request
,HttpServletResponse response
,HttpSession session
,Model model) {
Member member = memberService.getMemberByMemberId("1000");
model.addAttribute("member", member);
List memberList = memberService.queryAllMember();
model.addAttribute("memberList", memberList);
return "/view/memberList.html";
}
@RequestMapping("/listjson")
@ResponseBody
public List memberListJson (
HttpServletRequest request
,HttpServletResponse response
,HttpSession session
,Model model) {
model.addAttribute("userName", "zhangsan");
List memberList = memberService.queryAllMember();
return memberList;
}
}
2,相关试图 index.html 代码,
Insert title here
Home index page
spring boot database project
thymeleaf相关语法:
https://blog.csdn.net/zrk1000/article/details/72667478
springboot通过myBatis使用默认的JDBC链接MS SQL Server查询数据
【springboot+mybatis+jdbc+ms sql server】
查询会员ID为1000的用户
查询会员列表 [页面]
查询会员列表 [JSON]
试图 memberList.html 代码
Insert title here
Member list page
member ID:1000
循环列出用户列表member list:
九,访问,
如上所示,基本实现了在springboot中使用mybatis和默认的JDBC对数据库进行操作,欢迎拍砖...