写一个简易的java项目(一) 创建项目并连接数据库

写一个简易的项目

环境: jdk1.8

编辑器:idea 

用到的技术:springboot + mybatis-plus + mysql

1.创建项目

 2. jdk 就用我电脑上的1.8

 3. 改成自己想要的名字 因为准备打war包所以这里是war

 4. web 

 5. 习惯改成yml

  端口号 配置一下 ,

  前台页面的路径配置一下

server:
  port: 8091
  max-http-header-size: 10240
spring:
  mvc:
    view:
      prefix: /pages
View Code

6. 创建一个html

 7. 创建controller

 8. 展示 没问题

 # 9.  如果想打包的话 

  mvn clean package -Dmaven.test.skip=true

  包在这呦

 下一步-》》》》》连数据库

1. 引入pom

       <!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
View Code

 

 

 

 

 2.改yml

数据库连接池配置:

当然这之前要建表 。

spring:
  mvc:
    view:
      prefix: /pages
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mypro?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
View Code

 

 简单配一下 mybatis-plus: 

mybatis-plus:
    mapper-locations: classpath:/mapping/*.xml
View Code

 3. 把 service mapper 之类的都建好

看一下现在的目录

 

 看一下代码

controller

package com.example.king.hello.controller;

import com.example.king.hello.dto.Hello;
import com.example.king.hello.service.HelloService;
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
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private HelloService helloService;

    @RequestMapping("")
    public String index() {
        return "/hello.html";
    }

    @ResponseBody
    @RequestMapping("/getData")
    public List<Hello> getData() {
        return helloService.getData();
    }

}
View Code

service

package com.example.king.hello.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.king.hello.dto.Hello;

import java.util.List;

public interface HelloService extends IService<Hello> {

    List<Hello> getData();
}
View Code

serviceImpl

package com.example.king.hello.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.king.hello.dto.Hello;
import com.example.king.hello.mapper.HelloMapper;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class HelloServiceImpl extends ServiceImpl<HelloMapper, Hello> implements HelloService {


    @Override
    public List<Hello> getData() {
        QueryWrapper<Hello> wrapper = new QueryWrapper<>();
        return this.baseMapper.selectList(wrapper);
    }
}
View Code

实体类

package com.example.king.hello.dto;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;

@TableName("tab_hello")
public class Hello implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField("name")
    private String name;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
View Code

mapper

package com.example.king.hello.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.king.hello.dto.Hello;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper
public interface HelloMapper extends BaseMapper<Hello> {

}
View Code

为了测试表里只有两个字段 id 和name

看一下结果 

##如果需要写xml 可以这样配置:

pom 中加上

    <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
View Code

 

 配置文件中:

mybatis-plus:
  global-config:
    db-config:
      id-type: auto
      field-strategy: not_empty
      table-underline: true
      db-type: mysql
      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
  mapper-locations: classpath*:com/example/king/**/mapper/mapping/*.xml
View Code

 

 @

posted @ 2020-03-13 10:24  DarGi  阅读(1688)  评论(0编辑  收藏  举报