快捷搜索:  汽车  科技

使用ssm框架构建java web总结(搭建简易的网页)

使用ssm框架构建java web总结(搭建简易的网页)<dependency> <!--添加jsp依赖 -->依次点击,创建项目二.添加pom.xml依赖因为springboot不推荐使用jsp做页面,如果想要使用就必需的添加jsp的依赖

还是我的风格,话不多说,直接上干货。

开发工具:idea

开发环境:java8

使用ssm框架构建java web总结(搭建简易的网页)(1)

一.创建项目

使用ssm框架构建java web总结(搭建简易的网页)(2)

依次点击,创建项目

二.添加pom.xml依赖

因为springboot不推荐使用jsp做页面,如果想要使用就必需的添加jsp的依赖

<!--添加jsp依赖 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

</dependency>

三.spring boot整合spring mvc

因为在创建项目的时候我们选择了mybatis和jdbc所以在这里也要把他们两也给配置进去

1.编辑application.properties

# 页面默认前缀目录

spring.mvc.view.prefix=/WEB-INF/jsp/

# 响应页面默认后缀

spring.mvc.view.suffix=.jsp

#开发配置

spring.datasource.driverClassName = com.Mysql.jdbc.Driver

spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8

spring.datasource.username = root

spring.datasource.password = 123456

# mybatis接口文件位置

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

mybatis.type-aliases-package: com.chaoqi.springboot_demo2.domain

2编辑完application.properties之后再src/mian下创建webapp目录,结构如下

使用ssm框架构建java web总结(搭建简易的网页)(3)

目录结构

index.jsp文件内容如下 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> this is springboot </body> </html>

3新建IndexController

package com.chaoqi.springboot_test.web;

import org.springframework.stereotype.Controller;

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

@Controller

public class IndexController {

private static final String INDEX = "index";

@RequestMapping("/show")

public String getIndex() {

return INDEX;

}

}

4创建完成后整体结构如下图

使用ssm框架构建java web总结(搭建简易的网页)(4)

整体结构图

5运行main函数

使用ssm框架构建java web总结(搭建简易的网页)(5)

6访问页面 localhost:8080//show,成功

使用ssm框架构建java web总结(搭建简易的网页)(6)

四、springboot整合mybatis

1.创建数据表

Cmd命令行窗口。Mysql:Test数据库下创建新表并加入数据

建表

CREATE TABLE `music_info` (

`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id'

`singer_name` varchar(100) NOT NULL COMMENT '歌手名'

`music_size` varchar(100) NOT NULL COMMENT '歌曲大小'

`music_name` varchar(100) NOT NULL COMMENT '歌曲名'

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

插入三条数据

INSERT INTO `music_info` VALUES ('1' '小三' '3.2M' '起风了');

INSERT INTO `music_info` VALUES ('2' '刘德华' '3.0M' '忘情水');

INSERT INTO `music_info` VALUES ('3' '猪点点' '5.0M' '会写程序的小猪');

使用ssm框架构建java web总结(搭建简易的网页)(7)

2.创建pojo

package com.chaoqi.springboot_test.dao.domain;

public class MusicInfo {

// 主键id

private Integer id;

// 歌手名

private String singerName;

// 歌曲大小

private String musicSize;

// 歌曲名

private String musicName;

/**

* 获取 主键id music_info.id

*

* @return 主键id

*/

public Integer getId() {

return id;

}

/**

* 设置 主键id music_info.id

*

* @param id 主键id

*/

public void setId(Integer id) {

this.id = id;

}

/**

* 获取 歌手名 music_info.singer_name

*

* @return 歌手名

*/

public String getSingerName() {

return singerName;

}

/**

* 设置 歌手名 music_info.singer_name

*

* @param singerName 歌手名

*/

public void setSingerName(String singerName) {

this.singerName = singerName == null ? null : singerName.trim();

}

/**

* 获取 歌曲大小 music_info.music_size

*

* @return 歌曲大小

*/

public String getMusicSize() {

return musicSize;

}

/**

* 设置 歌曲大小 music_info.music_size

*

* @param musicSize 歌曲大小

*/

public void setMusicSize(String musicSize) {

this.musicSize = musicSize == null ? null : musicSize.trim();

}

/**

* 获取 歌曲名 music_info.music_name

*

* @return 歌曲名

*/

public String getMusicName() {

return musicName;

}

/**

* 设置 歌曲名 music_info.music_name

*

* @param musicName 歌曲名

*/

public void setMusicName(String musicName) {

this.musicName = musicName == null ? null : musicName.trim();

}

@Override

public String toString() {

return "MusicInfo{"

"id=" id

" singerName='" singerName '\''

" musicSize='" musicSize '\''

" musicName='" musicName '\''

'}';

}

}

3.创建mapper.xml

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

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.chaoqi.springboot_test.dao.mapper.MusicInfoMapper">

<resultMap id="BaseResultMap" type="com.chaoqi.springboot_test.dao.domain.MusicInfo">

<id column="id" jdbcType="INTEGER" property="id" />

<result column="singer_name" jdbcType="VARCHAR" property="singerName" />

<result column="music_size" jdbcType="VARCHAR" property="musicSize" />

<result column="music_name" jdbcType="VARCHAR" property="musicName" />

</resultMap>

</mapper>

4创建mapper

package com.chaoqi.springboot_test.dao.mapper;

import com.chaoqi.springboot_test.dao.domain.MusicInfo;

import org.apache.ibatis.annotations.ResultMap;

import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MusicInfoMapper {

@ResultMap("BaseResultMap")

@Select("select * from music_info")

List<MusicInfo> selectAll(MusicInfo musicInfo);

}

5.service接口

package com.chaoqi.springboot_test.service;

import com.chaoqi.springboot_test.dao.domain.MusicInfo;

import java.util.List;

public interface MusicInfoService {

public List<MusicInfo> getMusicInfo(MusicInfo musicInfo);

}

6.servise实现类

package com.chaoqi.springboot_test.service.impl;

import com.chaoqi.springboot_test.dao.domain.MusicInfo;

import com.chaoqi.springboot_test.dao.mapper.MusicInfoMapper;

import com.chaoqi.springboot_test.service.MusicInfoService;

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

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class MusicInfoServiceImpl implements MusicInfoService {

@Autowired

private MusicInfoMapper musicInfoMapper;

@Override

public List<MusicInfo> getMusicInfo(MusicInfo musicInfo) {

List<MusicInfo> musicInfos = musicInfoMapper.selectAll(null);

return musicInfos;

}

}

7.编辑indexcontroller

package com.chaoqi.springboot_test.web;

import com.chaoqi.springboot_test.dao.domain.MusicInfo;

import com.chaoqi.springboot_test.service.MusicInfoService;

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 java.util.List;

@Controller

public class IndexController {

private static final String INDEX = "index";

@Autowired

private MusicInfoService musicInfoService;

@RequestMapping("/show")

public String getIndex() {

return INDEX;

}

@RequestMapping("/music")

@ResponseBody

public List<MusicInfo> getMusicInfo(MusicInfo musicInfo) {

List<MusicInfo> musicInfoList = musicInfoService.getMusicInfo(null);

return musicInfoList;

}

}

8.给SpringbootTestApplication类加上注解@MapperScan("com.chaoqi.springboot_test.dao.mapper")

package com.chaoqi.springboot_test;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

@SpringBootApplication

@MapperScan("com.chaoqi.springboot_test.dao.mapper")

public class SpringbootTestApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootTestApplication.class args);

}

}

9.运行项目,成功,springboot springmvc mybatis整合完成

Localhost:8080/music

使用ssm框架构建java web总结(搭建简易的网页)(8)

猜您喜欢: