您的位置:首页 > 编程语言 > Java开发

Springboot 整合 SSM 入门

2020-02-01 16:43 609 查看

Springboot 整合 SSM 入门

首先是创建一个springboot项目

然后下面是pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.coship.springboot</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>

application.yml配置

server:
port: 80
mybatis:
mapper-locations: classpath:mapper/mysql/*.xml
config-location: classpath:mybatis-config.xml
type-aliases-package: com.coship.springboot.enetity
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8
username : root
password : root
hikari:
connection-test-query: SELECT 1

mybatis-config

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

<typeAliases>
<typeAlias alias="user" type="com.coship.springboot.enetity.User"/>
</typeAliases>

</configuration>

映射文件:

<?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">

<!-- namespace为dao的位置,在mybatis-config里面需要引入这个xml文件 -->
<mapper namespace="com.coship.springboot.dao.user.UserDao">

<sql id="base_user_column">
USERID,
USERNAME,
PASSWORD,
MAJOR,
BIRTHDAY
</sql>

<resultMap id="userResult" type="user">
<result property="userId" column="USERID" />
<result property="userName" column="USERNAME" />
<result property="password" column="PASSWORD" />
<result property="major" column="MAJOR" />
<result property="birthday" column="BIRTHDAY" />
</resultMap>

<select id="getUserById" parameterType="java.lang.Integer"
resultMap="userResult">
<!-- 检索sql文 -->
SELECT
<include refid="base_user_column" />
FROM
t_user
WHERE
USERID = #{userId}
</select>

<select id="getAllUser"  resultMap="userResult">
SELECT
<include refid="base_user_column" />
FROM
t_user
</select>

<update id="updateUser" parameterType="user">
UPDATE t_user
<set>
<if test="userName!=null and userName !='' ">
USERNAME = #{userName}
</if>
<if test="password!=null and password !='' ">
,PASSWORD = #{password}
</if>
<if test="major!=null and major !=''">
,MAJOR = #{major}
</if>
<if test="birthday!=null and birthday !='' ">
,BIRTHDAY = #{birthday}
</if>
</set>
WHERE USERID = #{userId}
</update>

<delete id="deleteUser" parameterType="java.lang.Integer">
DELETE FROM t_user
WHERE USERID = #{userId}
</delete>

<insert id="addUser" parameterType="user">
<!-- selectKey插入后 可以用user.getUserId()获取到id  没有selectKey就获取不到位NULL -->
<selectKey keyProperty="userId" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID() AS USERID
</selectKey>
insert into
t_user(USERNAME,PASSWORD,MAJOR,BIRTHDAY)
values
(#{userName},#{password},#{major},#{birthday})
</insert>

</mapper>

这些定义完之后就开始码代码了
controller层

package com.coship.springboot.controller.user;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.coship.springboot.enetity.User;
import com.coship.springboot.service.user.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

private static Logger log = LoggerFactory.getLogger(UserController.class);

@Autowired
private UserService userService;

@RequestMapping(value="/getUserById/{userId}",method=RequestMethod.GET)
public @ResponseBody User getUserById(@PathVariable Integer userId){
log.info("visit method getUserById in UserController");
if(userId==null){
return null;
}
User user = userService.getUserById(userId);
if (user==null) {
return  new User();
}
return user;
}

//@RequestMapping(value="/getAllUser",method=RequestMethod.GET)
@GetMapping("/getAllUser")
public @ResponseBody List<User> getAllUser(){
log.info("visit method getAllUser in UserController");
return userService.getAllUser();
}

//@RequestMapping(value="/addUser",method=RequestMethod.POST)
@PostMapping("/addUser")
public @ResponseBody String addUser(User user){
int result = userService.addUser(user);
if(result > 0){
log.info("返回的result:"+result);
log.info("返回的id:"+user.getUserId());
log.info("新增用户成功,用户信息为:"+user.toString());
}else{
log.info("新增用户失败");
return "fail";
}
return "addUserSuccess";
}

//@RequestMapping(value="/updateUser",method=RequestMethod.POST)
@PostMapping("/updateUser")
public @ResponseBody String updateUser(User user){
int result = userService.updateUser(user);
if(result > 0){
log.info("修改用户成功,修改后的用户信息为:"+user.toString());
}else{
log.info("修改用户信息失败");
return "fail";
}
return "updateUserSuccess";
}

//@RequestMapping(value="/deleteUser/{userId}",method=RequestMethod.GET)
@GetMapping("/deleteUser/{userId}")
public @ResponseBody String deleteUser(@PathVariable Integer userId){
int result = userService.deleteUser(userId);
if(result > 0){
log.info("删除用户成功");
}else{
log.info("删除用户失败");
return "fail";
}
return "deleteUserSuccess";
}
}

service层接口

package com.coship.springboot.service.user;

import java.util.List;

import com.coship.springboot.enetity.User;

public interface UserService {

public List<User> getAllUser();

public User getUserById(Integer userId);

public Integer updateUser(User user);

public Integer deleteUser(Integer userId);

public Integer addUser(User user);

}

service实现类

package com.coship.springboot.service.user.impl;

import java.util.List;

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

import com.coship.springboot.dao.user.UserDao;
import com.coship.springboot.enetity.User;
import com.coship.springboot.service.user.UserService;

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserDao userDao;

public List<User> getAllUser() {
return userDao.getAllUser();
}

public User getUserById(Integer userId) {
return userDao.getUserById(userId);
}

public Integer updateUser(User user) {
return userDao.updateUser(user);
}

public Integer deleteUser(Integer userId) {
return userDao.deleteUser(userId);
}

public Integer addUser(User user) {
return userDao.addUser(user);
}

}

dao层(mapper)

package com.coship.springboot.dao.user;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.coship.springboot.enetity.User;

@Mapper
public interface UserDao {

public List<User> getAllUser();

public User getUserById(Integer userId);

public Integer updateUser(User user);

public Integer deleteUser(Integer userId);

public Integer addUser(User user);

}

程序入口

package com.coship.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;

@SpringBootApplication
@Controller
public class HelloWorldController {

public static void main(String[] args) {
SpringApplication.run(HelloWorldController.class, args);
}
}

最后不要忘记在数据库设计表噢.
全部操作完之后就启动吧!
访问http://127.0.0.1/user/getAllUser

  • 点赞
  • 收藏
  • 分享
  • 文章举报
qq_37501896 发布了1 篇原创文章 · 获赞 0 · 访问量 55 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: