ssm 访问html id,SpringBoot 集成SSM(spring+springmvc+mabatis)

SpringBoot 集成SSM(spring+springmvc+mabatis)

发布时间:2018-05-26 17:56,

浏览次数:1405

, 标签:

SpringBoot

SSM

spring

springmvc

mabatis

SpringBoot 集成SSM(spring+springmvc+mabatis)

1.前言

该SpringBoot集成SSM的例子基于elipse环境开发,JDK版本为1.8,tomcat为Springboot自带的容器。

2.开发环境

操作系统:window10

开发工具:eclipse(eclipse-jee-oxygen-3a-win32-x86_64,eclipse_2018)

服务器:springboot自带的tomcat容器

插件:maven插件(可以自行配置,也可以直接使用springboot内置的maven插件)

3.集成SSM操作步骤

3.1创建基于maven的web项目

3.1.1.新建一个maven-web项目

3.1.2 填入GroupID和ArtifactID即可

GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。(例:com.ypf.cn)

ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。(例:my_springboot_ssm)

3.1.3创建后出错的的解决办法(若无错也可以通过此方法查看项目搭建后的环境是否正确)

通过Problems控制台查看错误现象

(注:若Problems控制台没有显示,可以通过Window->Show View->Other,在弹出框中搜索 Problems 点击Open即可)

根据错误提示,可通过Build Path配置环境解决

错误1:包丢失

错误1解决办法:

remove这两个包,然后在新建Source Folder,创建src/main/java和src/test/java形成标准的maven结构

错误2:JDK版本问题

安装的版本为1.8,配置的也是1.8,需修改这里1.5的JDK版本为配置的1.8版本

错误2解决办法:

点击Edit修改为1.8版本即可

错误3:项目运行时环境问题

错误3解决办法:

点击Finish即可

至此,所有错误都已解决啦……

3.2导入pom.xml(jar分析)

引入相关jar,点击maven->update Project即可

spring-boot-starter-aop

面向切面编程的支持,包括spring-aop和AspectJ

pring-boot-starter-web

对全栈web开发的支持,包括Tomcat和spring-webmvc

spring-boot-starter-jdbc

对JDBC数据库的支持

spring-boot-starter-test

SpringBoot Test单元测试

mybatis-spring-boot-starter

spring boot整合mybatis

spring-boot-starter-logging

springboot默认日志 Logback

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0

com.ypf.cnmy_springboot_ssm

war0.0.1-SNAPSHOT

my_springboot_ssm Maven Webapphttp://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.5.9.RELEASE

UTF-8

UTF-8

1.8org.springframework.boot

spring-boot-starter-jdbcmysql

mysql-connector-javaruntime

org.springframework.boot

spring-boot-starter-aoporg.springframework.boot

spring-boot-starter-testtest

org.mybatis.spring.boot

mybatis-spring-boot-starter1.1.1

org.springframework.boot

spring-boot-starter-logging

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-thymeleaf

net.sourceforge.nekohtmlnekohtml

org.springframework.boot

spring-boot-maven-plugin

3.3 创建数据库

新建mytest数据库,在库中创建user表,如下:

创建sql如下

SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure

for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE

TABLE `user` ( `id` int(64) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT

NULL, `age` int(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB

AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- --

Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1',

'张三', '16'); INSERT INTO `user` VALUES ('2', '李四', '24');

3.4创建项目结构(配置后可提供返回JSON的结构)

此项目结构如下

3.4.1 application.properties

SpringBoot配置文件支持application.properties和application.yml两种写法,两种方式作用一样,仅写法不同,这里才做

application.properties这种做法,稍后我也会把此项目application.yml的用法也写一份出来以供学习使用。

application.properties的写法

server.port=9996 server.tomcat.uri-encoding=utf-8 #MySQL

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/mytest?characterEncoding=utf8

spring.datasource.username=root spring.datasource.password=root

spring.jpa.database = mysql #Mybatis

mybatis.mapper-locations=classpath*:mapper/*Mapper.xml

logging.level.org.springframework=WARN logging.level.com.yfp.cn=DEBUG

logging.file=logs/spring-boot-logging.log #remove thymeleaf Strict check

spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false

spring.thymeleaf.prefix=/WEB-INF/ spring.thymeleaf.suffix=.html

#spring.thymeleaf.content-type=text/html # don't use it because it's

inoperative #spring.mvc.view.prefix=/views/ #spring.mvc.view.suffix=.html

#spring.mvc.view.prefix=/WEB-INF/views/ #spring.mvc.view.suffix=.jsp

application.yml的写法

3.4.2 MySpringBootApplication

该类是SpringBoot的核心启动类

MySpringBootApplication.java

package com.ypf.cn; import org.springframework.boot.SpringApplication; import

org.springframework.boot.autoconfigure.SpringBootApplication; import

org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication @EnableTransactionManagement//开启事务管理 public class

MySpringBootApplication { public static void main(String[] args) {

SpringApplication.run(MySpringBootApplication.class, args); } }

3.4.3 UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?> /p>

"-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

namespace="com.ypf.cn.dao.UserMapper" >

resultType="com.ypf.cn.domain.User"> SELECT * FROM user

3.4.4 User.java

User.java

package com.ypf.cn.domain; public class User { private Integer id; private

String name; private Integer age; 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; } public Integer

getAge() { return age; } public void setAge(Integer age) { this.age = age; } }

3.4.5 UserMapper.java

UserMapper.java

package com.ypf.cn.dao; import java.util.List; import

org.apache.ibatis.annotations.Mapper; import

org.springframework.stereotype.Repository; import com.ypf.cn.domain.User;

@Mapper //声明是一个Mapper,与springbootApplication中的@MapperScan二选一写上即可 @Repository

public interface UserMapper{ List getAll(); }

3.4.6 UserService.java

UserService.java

package com.ypf.cn.service; import java.util.List; import

org.springframework.beans.factory.annotation.Autowired; import

org.springframework.stereotype.Service; import com.ypf.cn.dao.UserMapper;

import com.ypf.cn.domain.User; @Service public class UserService implements

UserMapper{ @Autowired UserMapper userMapper; @Override public List

getAll() { return userMapper.getAll(); } }

3.4.7 UserController.java

UserController.java

package com.ypf.cn.controller; import java.util.List; import

org.springframework.beans.factory.annotation.Autowired; import

org.springframework.stereotype.Controller; import

org.springframework.web.bind.annotation.RequestMapping; import

org.springframework.web.bind.annotation.ResponseBody; import

com.ypf.cn.domain.User; import com.ypf.cn.service.UserService; @Controller

@RequestMapping("user") public class UserController { //依赖注入 @Autowired

UserService userService; @RequestMapping(value = "getAll")//method

不写的话,默认GET、POST都支持,根据前端方式自动适应

/*@GetMapping("getAll")//@GetMapping是一个组合注解,是@RequestMapping(method =

RequestMethod.GET)的缩写。

@PostMapping("getAll")//@PostMapping是一个组合注解,是@RequestMapping(method =

RequestMethod.POST)的缩写。*/ @ResponseBody public List getAll() { //调用dao层

List all = userService.getAll(); System.out.println(all); return all; }

@RequestMapping("/login") public String login() { return "/login"; }

@RequestMapping("/login2") public String login2() { return "views/login"; }

@RequestMapping("/user") public String login3() { return "views/user/user"; }

@RequestMapping("/dept") public String login4() { return "views/dept/dept"; } }

3.5 返回视图(返回html页面)

返回视图的结构如下

需要配置application.properties

spring.thymeleaf.prefix=/WEB-INF/

spring.thymeleaf.suffix=.html

3.5.1 user.html

Insert title

here

webapp/WEB-INF/views/user/user.html

3.5.2 dept.html

Insert title

here

webapp/WEB-INF/views/dept/dept.html

3.5.3 login.html

Insert title

here

webapp/WEB-INF/views/login
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值