JavaEE-自定义SSM-环境搭建-测试程序

1. 环境搭建

1.1 结构说明

JavaEE-自定义SSM-环境搭建-测试程序_第1张图片

1.2 创建父项目

  • 项目名:myssm-parent-quan12

JavaEE-自定义SSM-环境搭建-测试程序_第2张图片

1.3 创建核心项目

  • 项目名:myssm-core-quan12

    JavaEE-自定义SSM-环境搭建-测试程序_第3张图片

  • 配置pom文件(4个坐标)

    
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-tomcatartifactId>
                <version>2.3.12.RELEASEversion>
            dependency>
            
            <dependency>
                <groupId>org.yamlgroupId>
                <artifactId>snakeyamlartifactId>
                <version>1.26version>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>1.18.24version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>1.2.83version>
            dependency>
        dependencies>
    
    

1.4 测试项目

  • 项目名:myssm-test-quan12

    JavaEE-自定义SSM-环境搭建-测试程序_第4张图片

  • 配置pom文件(2个坐标)

        <dependencies>
            
            <dependency>
                <groupId>com.czxygroupId>
                <artifactId>myssm-core-quan12artifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.32version>
            dependency>
        dependencies>
    

2. 编写测试程序

2.0 编写流程

  • 编写内容:
    • yml文件
    • 启动类:@MySpringBootApplication、MySpringApplication.run()
    • domain
    • 数据访问层dao:@MyMapper
    • 业务层service:@MyService
    • 控制器web:@MyRestController、@MyRequestMapping、@MyGetMapping、@MyPostMapping
  • 【注意】测试程序编写完成后,将会出现大量错误,核心core没有完成任何功能

2.1 编写yml文件

JavaEE-自定义SSM-环境搭建-测试程序_第5张图片

server:
  port: 9090
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/cloud_db1?useUnicode=true&characterEncoding=utf8
    username: root
    password: '1234'

2.2 启动类

JavaEE-自定义SSM-环境搭建-测试程序_第6张图片

package com.czxy;


@MySpringBootApplication
public class MyTestApplication {
    public static void main(String[] args) {
        MySpringApplication.run(MyTestApplication.class, args);
    }
}

  • 注意:程序报错属于正常现象,后面核心编写完成后将解决。

    JavaEE-自定义SSM-环境搭建-测试程序_第7张图片

2.3 JavaBean:User

JavaEE-自定义SSM-环境搭建-测试程序_第8张图片

package com.czxy.domain;

import lombok.Data;


@Data
public class User {
    private Integer uid;
    private String username;
    private String password;
    private Integer age;
}

/*
CREATE TABLE `t_user` (
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `password` varchar(32) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`uid`)
)
 */

2.4 Mapper

JavaEE-自定义SSM-环境搭建-测试程序_第9张图片

package com.czxy.mapper;

import com.czxy.domain.User;

import java.util.List;


@MyMapper
public interface UserMapper {

    @MySelect("select * from t_user")
    public List<User> selectAll();
}

2.5 Service

JavaEE-自定义SSM-环境搭建-测试程序_第10张图片

  • 接口

    package com.czxy.service;
    
    import com.czxy.domain.User;
    
    import java.util.List;
    
    
    public interface UserService {
    
        public List<User> selectAll();
    }
    
    
  • 实现类

    package com.czxy.service.impl;
    
    import com.czxy.domain.User;
    import com.czxy.mapper.UserMapper;
    import com.czxy.service.UserService;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    
    @MyService
    public class UserServiceImpl implements UserService {
    
        @Resource
        private UserMapper userMapper;
    
        @Override
        public List<User> selectAll() {
            return userMapper.selectAll();
        }
    }
    
    

2.6 Controller

JavaEE-自定义SSM-环境搭建-测试程序_第11张图片

package com.czxy.controller;

import com.czxy.domain.User;
import com.czxy.service.UserService;

import javax.annotation.Resource;
import java.util.List;


@MyRestController
@MyRequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @MyGetMapping
    public List<User> selectAll() {
        List<User> userList = userService.selectAll();
        return userList;
    }
}

你可能感兴趣的:(java,java-ee,java)