写一个简易的项目
环境: jdk1.8
编辑器:idea
用到的技术:springboot + mybatis-plus + mysql
1.创建项目
2. jdk 就用我电脑上的1.8
3. 改成自己想要的名字 因为准备打war包所以这里是war
4. web
5. 习惯改成yml
端口号 配置一下 ,
前台页面的路径配置一下
server:
port:8091max-http-header-size: 10240spring:
mvc:
view:
prefix:/pages
View Code
6. 创建一个html
7. 创建controller
8. 展示 没问题
# 9. 如果想打包的话
mvn clean package -Dmaven.test.skip=true
包在这呦
下一步-》》》》》连数据库
1. 引入pom
com.baomidou
mybatis-plus-boot-starter
3.1.0
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
com.alibaba
druid-spring-boot-starter
1.1.10
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
packagecom.example.king.hello.controller;importcom.example.king.hello.dto.Hello;importcom.example.king.hello.service.HelloService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importjava.util.List;
@Controller
@RequestMapping("/hello")public classHelloController {
@AutowiredprivateHelloService helloService;
@RequestMapping("")publicString index() {return "/hello.html";
}
@ResponseBody
@RequestMapping("/getData")public ListgetData() {returnhelloService.getData();
}
}
View Code
service
packagecom.example.king.hello.service;importcom.baomidou.mybatisplus.extension.service.IService;importcom.example.king.hello.dto.Hello;importjava.util.List;public interface HelloService extends IService{
ListgetData();
}
View Code
serviceImpl
packagecom.example.king.hello.service;importcom.baomidou.mybatisplus.core.conditions.Wrapper;importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;importcom.baomidou.mybatisplus.extension.service.impl.ServiceImpl;importcom.example.king.hello.dto.Hello;importcom.example.king.hello.mapper.HelloMapper;importorg.springframework.stereotype.Service;importjavax.transaction.Transactional;importjava.util.List;
@Service
@Transactionalpublic class HelloServiceImpl extends ServiceImpl implementsHelloService {
@Overridepublic ListgetData() {
QueryWrapper wrapper = new QueryWrapper<>();return this.baseMapper.selectList(wrapper);
}
}
View Code
实体类
packagecom.example.king.hello.dto;importcom.baomidou.mybatisplus.annotation.IdType;importcom.baomidou.mybatisplus.annotation.TableField;importcom.baomidou.mybatisplus.annotation.TableId;importcom.baomidou.mybatisplus.annotation.TableName;importjava.io.Serializable;
@TableName("tab_hello")public class Hello implementsSerializable {private static final long serialVersionUID = 1L;
@TableId(value= "id", type =IdType.AUTO)privateLong id;
@TableField("name")privateString name;publicLong getId() {returnid;
}public voidsetId(Long id) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}
@OverridepublicString toString() {return "Hello{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
View Code
mapper
packagecom.example.king.hello.mapper;importcom.baomidou.mybatisplus.core.mapper.BaseMapper;importcom.example.king.hello.dto.Hello;importorg.apache.ibatis.annotations.Mapper;importorg.springframework.stereotype.Repository;
@Mapperpublic interface HelloMapper extends BaseMapper{
}
View Code
为了测试表里只有两个字段 id 和name
看一下结果
##如果需要写xml 可以这样配置:
pom 中加上
src/main/java
**/*.xml
View Code
配置文件中:
mybatis-plus:global-config:
db-config:
id-type: auto
field-strategy: not_empty
table-underline: truedb-type: mysql
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
mapper-locations: classpath*:com/example/king/**/mapper/mapping/*.xml
View Code
@