Java实践——小程序的开发

前言

视频网站:视频与笔记

1.环境的搭建

  1. JDK(8,11,13,17,21版本皆可)

    1
    2
    3
    环境变量:
    JAVA_HOME:JDK路径
    path:%JAVA_HOME%\bin (关联javac.exe,java.exe)
  2. Idea安装:2020 社区版,旗舰版

  3. MySQL:5/8

  4. navicat:不破解试用14天(破解注意关闭防火墙)

  5. Maven:项目管理工具(解压即可)

    • 创建本地仓库repository空⽂件夹(注意:与Maven在同一目录)

      001

    • 在Maven目录先/conf/settings.xml里,在53行左右插入:

      1
      <localRepository>本地路径/repository</localRepository>
    • 配置阿里云镜像,在161行插入:

      1
      2
      3
      4
      5
      6
      <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      </mirror>
    • 保存

2.创建web项目

2.1 创建web项目

2.2 Idea的配置

  • 自动配置

    002

  • 不区分大小写

    002

  • maven配置

    002

  1. 在pom.xml文件下修改以下两个版本号,运行项目

    002

3.pom.xml结构

  1. 依赖:

    002

  2. MySQL的依赖

    1
    2
    3
    4
    5
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.29</version>
    </dependency>
  3. 找依赖的网址:https://mvnrepository.com/

  4. maven管理项目

    002

4.项目的构建

4.1 项目的执行流程

002

4.2 项目的构建

在demo下创建5个包:config,controller,dao,entity,service

002

5.controller交互springmvc

  • 拦截请求
  • 导入springmvc依赖(已有)
1
2
3
4
><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
></dependency>
  • 创建类并且添加controller注解
1
>@Controller  //拦截请求
  • @RequestMapping()适配请求地址。——> restful设计风格
1
2
3
4
5
6
7
8
9
10
11
12
>/**
* url: http://localhost:8080/test
*
* restful设计风格(请求方式)
*增 post
*删 delete
*改 put
*查 get
*/

>@RequestMapping("test") //第一级适配地址请求地址

注:当端口号被占用的时候,在resource/application.properties文件中添加

1
>server.port=8081

下载apifox软件

  • 获取参数
  • 获取参数——>获取用户请求数据
  • 参数的名称保持⼀致。key——value键值对
  • 路径传参 @PathVariable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@GetMapping("one")
public void getTest(String username,String password){
System.out.println("查询单个信息");
System.out.println(username+password);
}

@GetMapping("all/{name}/{word}")
public void getAllTest(@PathVariable String name,@PathVariable String word){
System.out.println("查询所有信息");
System.out.println(name + word);
}

@PostMapping
public void addTest(){
System.out.println("增加信息");
}

@DeleteMapping
public void deleteTest(){
System.out.println("删除信息");
}

@PutMapping
public void updataTest(){
System.out.println("更新信息");
}
  • 响应结果
  • 响应⻚⾯/数据(给前端返回数据,前后端不分离)

  • 返回页面

    • 导⼊thymeleaf模版引擎依赖。

      1
      2
      3
      4
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
    • 创建新⻚⾯——>存放在templates⽂件中

      创建一个index.html

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>首页</title>
      </head>
      <body>
      hello world
      </body>
      </html>
    • 定义返回⻚⾯的⽅法

      url:http://localhost:8080/user/topage

      return”⻚⾯名称”;

      1
      2
      3
      4
      5
      6
      7
      /**
      * 返回主页面
      */
      @RequestMapping("topage")
      public String toPage(){
      return "index";
      }
  • 返回数据

    • 添加注解@ResponseBody(返回数据为JSON、XML)

    • url:http://localhost:8080/user/todata

      1
      2
      3
      4
      5
      6
      7
      8
      /**
      * 返回数据
      */
      @RequestMapping("data")
      @ResponseBody
      public String toData(){
      return "index";
      }
  • 封装实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

// 属性
private int id;

private String username;

private String password;

private String sex;

// 构造
public User(){

}

public User(int id, String username, String password, String sex) {
this.id = id;
this.username = username;
this.password = password;
this.sex = sex;
}

// get,set

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}


//重写tostring

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
'}';
}
  • JSON参数的获取
1
2
3
4
5
6
7
8
9
10
//当传参的时候,传入JSON参数要使用
@RequestBody

//例如:
@RequestMapping("insert")
@ResponseBody
public User addTest(@RequestBody User user){
System.out.println("增加信息");
return user;
}
  • 调用业务(要有业务层)

登录业务 (⽤户user)

url:http://localhost:8080/user/login?username=admin&password=123456

UserController拦截请求

创建业务层

创建接⼝定义⽅法

创建实现类实现⽅法

业务层对象的实例化(new 对象) @Autowired

调⽤⽅法得到结果

002

6.JDBC链接数据库

002

  1. 安装Navicat,并且创建数据库

  2. 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
       
    public static void main(String[] args) {

    try {
    Driver driver = new Driver();
    DriverManager.registerDriver(driver);
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo01?serverTimezone=Asia/Shanghai", "root", "root");

    PreparedStatement preparedStatement = connection.prepareStatement("select * from t_user");
    ResultSet resultSet = preparedStatement.executeQuery();

    while (resultSet.next()){
    int id = resultSet.getInt(1);
    String name = resultSet.getString(2);
    String password = resultSet.getString(3);
    String sex = resultSet.getString(4);
    User user = new User(id, name, password, sex);//封装对象
    System.out.println(user);

    }

    } catch (Exception e) {
    throw new RuntimeException(e);
    }


    }

7.mybatis半映色框架

mybatis官网

  • 配置MySQL(在全局文件)

    1
    2
    3
    4
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/demo01?serverTimezone=Asia/Shanghai
    spring.datasource.username=root
    spring.datasource.password=root
  • 导入mybatis依赖

    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
    </dependency>
  • 创建dao

    002

  • dao.xml文件的基础配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?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:映射具体接口的全限定类名-->
    <mapper namespace="com.qf.demo002.dao.ScoreDao">


    </mapper>


  • 配置 扫描dao 和mapper

    1
    2
    3
    4
    5
    //Application文件
    @MapperScan("com.qf.demo002.dao")

    //全局文件
    mybatis.mapper-locations=classpath:mapper/*.xml

8.基本登录API的实现

Url:http://localhost:8080/user/login?username=admin&password=123456

参数:username=admin&password=123456

  • controller user==》login()
1
2
3
4
5
6
7
8
9
>@RequestMapping("login")
>@ResponseBody
>public String login(String username,String password){
>//调⽤业务 得到结果
>//UserService userService = new UserServiceImpl();
>String result = userService.login(username, password);
>//响应结果
>return result;
>}

Service

  • interface ==》定义⽅法名
1
>String login(String username,String password);
  • impl 实现类 ==》具体业务的实现(interface的⽅法)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>@Service //==>new对象 ==》new UserServiceImpl()
>public class UserServiceImpl implements UserService {
>@Autowired
>private UserDao userDao;
>@Override
>public String login(String username, String password) {
>//具体业务的实现
>//判断⽤户是否存在 通过⽤户名(客户端输⼊的⽤户名) 获取数据库⽤户信息(dao 链接数据库与
>数据库进⾏交互)
>User user = userDao.login(username);
>if (user!=null){
>//判断密码是否正确
>if (password.equals(user.getPassword())){
>return "success";
}else{
>return "密码错误";
}
}else{
return "⽤户不存在";
}
}
}
  • userDao.interface =>定义⽅法名
1
2
3
4
>public interface UserDao {
>//通过⽤户名获取⽤户信息
>User login(String name);
}
  • UserDao.xml
1
2
3
4
5
6
7
8
9
10
11
12
><?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:映射具体接口的全限定类名-->

><mapper namespace="com.hqh.demo001.dao.UserDao">
<!-- id:对应⽅法名 resultType:返回结果类型 (集合返回它的范型) #{参数名(⼀致)}-->
<select id="login" resultType="com.hqh.demo001.entity.User">
select * from t_user where username=#{name}
</select>
></mapper>

9.新建表,实现各类⽂件的创建

002

  • 封装实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
>package com.hqh.demo001.entity;

>public class TScore {

private int id;

private String cname;

private String idcard;

private int score;

public TScore() {
}

public TScore(int id, String cname, String idcard, int score) {
this.id = id;
this.cname = cname;
this.idcard = idcard;
this.score = score;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getCname() {
return cname;
}

public void setCname(String cname) {
this.cname = cname;
}

public String getIdcard() {
return idcard;
}

public void setIdcard(String idcard) {
this.idcard = idcard;
}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
}

@Override
public String toString() {
return "TScore{" +
"id=" + id +
", cname='" + cname + '\'' +
", idcard='" + idcard + '\'' +
", score=" + score +
'}';
}
>}

  • 创建各层类
  1. ScoreController
1
2
3
4
@Controller
@RequestMapping("score")
public class ScoreController {
}
  1. ScoreService
1
2
public interface ScoreService {
}
  1. ScoreServiceImpl
1
2
3
@Service
public class ScorceServiceImpl implements ScoreService {
}
  1. ScoreDao
1
2
public interface ScoreDao {
}
  1. ScoreDao.xml
1
2
3
4
5
6
7
<?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:映射具体接⼝的全限定类名-->
<mapper namespace="com.qf.demo002.dao.ScoreDao">
</mapper>

10.实现增删改查的api

  1. 查询所有
  1. 根据id查询成绩信息
  1. 通过id删除单条成绩
  1. 修改成绩信息
  • url:http://localhost:8080/score/update

  • 参数:score对象. 获取json格式数据(对象) @RequestBody

  • 返回值:String

    1
    2
    3
    4
    5
    6
    {
    "id":1,
    "cname":"⼆班",
    "idcard":"2022102007",
    "score":99
    }
  1. 增添单个成绩
1
2
3
4
5
6
{
"id":1,
"cname":"⼆班",
"idcard":"2022102008",
"score":98
}

12.前端

  1. 注册
  2. 官方开发文档
  3. 图标获取
  4. WXML文档

13.项目实现

  1. 数据库的导入

002

002

  1. 项目的分析

002

  1. 设计API
  1. 实现API
  • 封装实体类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    package com.hqh.demo001.entity;

    public class TImage {
    private int id;
    private String imageurl;
    private String imagetitle;
    private String imagetype;
    private String imagestate;

    public TImage() {
    }

    public TImage(int id, String imageurl, String imagetitle, String imagetype, String imagestate) {
    this.id = id;
    this.imageurl = imageurl;
    this.imagetitle = imagetitle;
    this.imagetype = imagetype;
    this.imagestate = imagestate;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getImageurl() {
    return imageurl;
    }

    public void setImageurl(String imageurl) {
    this.imageurl = imageurl;
    }

    public String getImagetitle() {
    return imagetitle;
    }

    public void setImagetitle(String imagetitle) {
    this.imagetitle = imagetitle;
    }

    public String getImagetype() {
    return imagetype;
    }

    public void setImagetype(String imagetype) {
    this.imagetype = imagetype;
    }

    public String getImagestate() {
    return imagestate;
    }

    public void setImagestate(String imagestate) {
    this.imagestate = imagestate;
    }

    @Override
    public String toString() {
    return "TImage{" +
    "id=" + id +
    ", imageurl='" + imageurl + '\'' +
    ", imagetitle='" + imagetitle + '\'' +
    ", imagetype='" + imagetype + '\'' +
    ", imagestate='" + imagestate + '\'' +
    '}';
    }
    }

  • 创建控制层

    1
    2
    3
    4
    5
    6
    7
    8
    @Controller
    @RequestMapping("image")
    public class ImageController {

    @Autowired
    private ImageService imageService;
    }

  • 创建业务层

    1. 接口

      1
      2
      public interface ImageService {
      }
    2. 实现类

      1
      2
      3
      4
      5
      @Service
      public class ImageServiceImpl implements ImageService {
      @Autowired
      private ImageDao imageDao;
      }
  • 创建数据持久层

    1. 接口

      1
      2
      public interface ImageDao {
      }
    2. 映射文件(接口的实现)

      1
      2
      3
      4
      5
      6
      7
      <?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:映射具体接⼝的全限定类名-->
      <mapper namespace="com.qf.demo002.dao.ImageDao">
      </mapper>

以下是整个项目的代码:

  • 首先是Demo001Application文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.hqh.demo001;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.hqh.demo001.dao")
public class Demo001Application {

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

}

  • entity文件夹下:
  1. TImage文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    package com.hqh.demo001.entity;

    public class TImage {
    private int id;
    private String imageurl;
    private String imagetitle;
    private String imagetype;
    private String imagestate;

    public TImage() {
    }

    public TImage(int id, String imageurl, String imagetitle, String imagetype, String imagestate) {
    this.id = id;
    this.imageurl = imageurl;
    this.imagetitle = imagetitle;
    this.imagetype = imagetype;
    this.imagestate = imagestate;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getImageurl() {
    return imageurl;
    }

    public void setImageurl(String imageurl) {
    this.imageurl = imageurl;
    }

    public String getImagetitle() {
    return imagetitle;
    }

    public void setImagetitle(String imagetitle) {
    this.imagetitle = imagetitle;
    }

    public String getImagetype() {
    return imagetype;
    }

    public void setImagetype(String imagetype) {
    this.imagetype = imagetype;
    }

    public String getImagestate() {
    return imagestate;
    }

    public void setImagestate(String imagestate) {
    this.imagestate = imagestate;
    }

    @Override
    public String toString() {
    return "TImage{" +
    "id=" + id +
    ", imageurl='" + imageurl + '\'' +
    ", imagetitle='" + imagetitle + '\'' +
    ", imagetype='" + imagetype + '\'' +
    ", imagestate='" + imagestate + '\'' +
    '}';
    }
    }

  2. Project文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    package com.hqh.demo001.entity;

    public class Project {
    private int id;

    private String proexplain;

    private Double proprice;

    private String prostep;

    private int protypeid;

    private String proname;

    private int imageid;

    private String prostatus;

    private int busid;

    private int tecid;

    private String imageurl;

    public Project() {
    }

    public Project(int id, String proexplain, Double proprice, String prostep, int protypeid, String proname, int imageid, String prostatus, int busid, int tecid, String imageurl) {
    this.id = id;
    this.proexplain = proexplain;
    this.proprice = proprice;
    this.prostep = prostep;
    this.protypeid = protypeid;
    this.proname = proname;
    this.imageid = imageid;
    this.prostatus = prostatus;
    this.busid = busid;
    this.tecid = tecid;
    this.imageurl = imageurl;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getProexplain() {
    return proexplain;
    }

    public void setProexplain(String proexplain) {
    this.proexplain = proexplain;
    }

    public Double getProprice() {
    return proprice;
    }

    public void setProprice(Double proprice) {
    this.proprice = proprice;
    }

    public String getProstep() {
    return prostep;
    }

    public void setProstep(String prostep) {
    this.prostep = prostep;
    }

    public int getProtypeid() {
    return protypeid;
    }

    public void setProtypeid(int protypeid) {
    this.protypeid = protypeid;
    }

    public String getProname() {
    return proname;
    }

    public void setProname(String proname) {
    this.proname = proname;
    }

    public int getImageid() {
    return imageid;
    }

    public void setImageid(int imageid) {
    this.imageid = imageid;
    }

    public String getProstatus() {
    return prostatus;
    }

    public void setProstatus(String prostatus) {
    this.prostatus = prostatus;
    }

    public int getBusid() {
    return busid;
    }

    public void setBusid(int busid) {
    this.busid = busid;
    }

    public int getTecid() {
    return tecid;
    }

    public void setTecid(int tecid) {
    this.tecid = tecid;
    }

    public String getImageurl() {
    return imageurl;
    }

    public void setImageurl(String imageurl) {
    this.imageurl = imageurl;
    }

    @Override
    public String toString() {
    return "Project{" +
    "id=" + id +
    ", proexplain='" + proexplain + '\'' +
    ", proprice=" + proprice +
    ", prostep='" + prostep + '\'' +
    ", protypeid=" + protypeid +
    ", proname='" + proname + '\'' +
    ", imageid=" + imageid +
    ", prostatus='" + prostatus + '\'' +
    ", busid=" + busid +
    ", tecid=" + tecid +
    ", imageurl='" + imageurl + '\'' +
    '}';
    }
    }

  • controller文件夹下:
  1. ImageController

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package com.hqh.demo001.controller;

    import com.hqh.demo001.entity.TImage;
    import com.hqh.demo001.service.ImageService;
    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.ArrayList;

    @Controller
    @RequestMapping("image")
    public class ImageController {

    @Autowired
    private ImageService imageService;

    @RequestMapping("getByImageType")
    @ResponseBody
    public ArrayList<TImage> getByImageType(String imagetype){
    ArrayList<TImage> tImages = imageService.getByImageType(imagetype);
    return tImages;
    }


    }

  2. ProjectController

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    package com.hqh.demo001.controller;

    import com.hqh.demo001.entity.Project;
    import com.hqh.demo001.service.ProjectService;
    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.ArrayList;

    @Controller
    @RequestMapping("project")
    public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @RequestMapping("getAll")
    @ResponseBody
    public ArrayList<Project> getAll(){
    ArrayList<Project> projects = projectService.getAll();
    return projects;
    }

    @RequestMapping("getByType")
    @ResponseBody
    public ArrayList<Project> getByType(String name){
    ArrayList<Project> projects = projectService.getByType(name);
    return projects;
    }

    @RequestMapping("getById")
    @ResponseBody
    public Project getById(int id){
    Project project = projectService.getById(id);
    return project;
    }

    }

  • service文件夹下:
  1. ImageService接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package com.hqh.demo001.service;

    import com.hqh.demo001.entity.TImage;

    import java.util.ArrayList;

    public interface ImageService {
    ArrayList<TImage> getByImageType(String imagetype);
    }

  2. ProjectController接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.hqh.demo001.service;

    import com.hqh.demo001.entity.Project;

    import java.util.ArrayList;

    public interface ProjectService {
    ArrayList<Project> getAll();

    ArrayList<Project> getByType(String name);

    Project getById(int id);
    }

  3. impl文件夹下

    • ImageServiceImpl

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      package com.hqh.demo001.service.impl;

      import com.hqh.demo001.dao.ImageDao;
      import com.hqh.demo001.entity.TImage;
      import com.hqh.demo001.service.ImageService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      import java.util.ArrayList;

      @Service
      public class ImageServiceImpl implements ImageService {
      @Autowired
      private ImageDao imageDao;

      @Override
      public ArrayList<TImage> getByImageType(String imagetype) {
      ArrayList<TImage> tImages = imageDao.getByImageType(imagetype);
      return tImages;
      }
      }

    • PrjeceServicrImpl

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      package com.hqh.demo001.service.impl;

      import com.hqh.demo001.dao.ProjectDao;
      import com.hqh.demo001.entity.Project;
      import com.hqh.demo001.service.ProjectService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      import java.util.ArrayList;

      @Service
      public class PrjeceServicrImpl implements ProjectService {
      @Autowired
      private ProjectDao projectDao;

      @Override
      public ArrayList<Project> getAll() {
      ArrayList<Project> projects = projectDao.getAll();
      return projects;
      }

      @Override
      public ArrayList<Project> getByType(String name) {
      ArrayList<Project> projects = projectDao.getByType(name);
      return projects;
      }

      @Override
      public Project getById(int id) {
      Project project = projectDao.getById(id);
      return null;
      }
      }

  • dao文件夹下
1
2
3
4
5
6
7
8
9
10
11
package com.hqh.demo001.dao;

import com.hqh.demo001.entity.TImage;

import java.util.ArrayList;


public interface ImageDao {
ArrayList<TImage> getByImageType(String imagetype);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.hqh.demo001.dao;

import com.hqh.demo001.entity.Project;

import java.util.ArrayList;

public interface ProjectDao {
ArrayList<Project> getAll();

ArrayList<Project> getByType(String name);

Project getById(int id);
}

  • mapper文件夹下
1
2
3
4
5
6
7
8
9
10
11
12
<?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:映射具体接⼝的全限定类名-->
<mapper namespace="com.hqh.demo001.dao.ImageDao">

<select id="getByImageType" resultType="com.hqh.demo001.entity.TImage">
select * from image where imagetype=#{imagetype}
</select>
</mapper>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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:映射具体接⼝的全限定类名-->
<mapper namespace="com.hqh.demo001.dao.ProjectDao">
<select id="getAll" resultType="com.hqh.demo001.entity.Project">
select * from project,image where project.imageid=image.id
</select>

<select id="getByType" resultType="com.hqh.demo001.entity.Project">
select * from project,image,projecttype where project.imageid=image.id and project.projecttype.id and project.`name`=#{name}
</select>

<select id="getById" resultType="com.hqh.demo001.entity.Project">
select * from project where id=#{id}
</select>


</mapper>

  1. 前端静态页面的实现

  2. 在后端的static目录下创建images,并将后端的图片放进去(删除原有的target目录,重新编译)

  3. 创建新项目,并且删除原有pages中的文件,app.js,app.json

  4. 前端新建images文件夹下

  5. tabbar的实现

    • 创建pages页面(app.json),[index,tech,my,detail],注意index文件夹(要有4个文件)

    • app.json:

      tabBat(回车)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      "tabBar": {
      "list": [{
      "pagePath": "pages/index/index",
      "text": "首页",
      "iconPath": "/images/index_icon.png",
      "selectedIconPath": "/images/index_icon_HL.png"
      },
      {
      "pagePath": "pages/tech/tech",
      "text": "技师",
      "iconPath": "/images/skill_icon.png",
      "selectedIconPath": "/images/skill_icon_HL.png"
      },
      {"pagePath": "pages/my/my",
      "text": "我的",
      "iconPath": "/images/user_icon.png",
      "selectedIconPath": "/images/user_icon_HL.png"

      }
      ]
      },
  6. 页面的实现

    • 轮播图的实现
  7. 前后端整合