IDEA之快速搭建SpringBoot+MyBatis项目
在以前开始学习框架的时候,都是在pom.xml文件上一点一点导入相关的包,太过繁琐。下面说一下如何使用idea快速搭建SpringBoot+MyBatis项目(干净利索快)目录一、创建项目二、环境配置2.1 修改配置文件2.2 mybatis配置文件三、创建映射器3.1 创建Bean3.2 定义Dao接口3.3 Controller3.4 静态数据四、启动项目五、测试一、创建项目打开idea,选择
在以前开始学习框架的时候,都是在pom.xml文件上一点一点导入相关的包,太过繁琐。
下面说一下如何使用idea快速搭建SpringBoot+MyBatis项目(干净利索快)
目录
一、创建项目
打开idea,选择创建项目(以idea 2020.1.2示例)
选择Spring Initializr
选择所需的模块(右边会展示你已选中的模块)
编辑项目名称,点击完成
这样idea就会自动导入依赖包,以及做好基础目录
就这样,项目就创建完了。
二、环境配置
2.1 修改配置文件
虽然idea自动生成的配置文件是application.properties,但是我个人还是自己创建了yml(SpringBoot底层会把application.yml文件解析为application.properties)
在创建项目时,我们添加了mysql、mybatis组件,所以需要配置数据源,否则项目启动会报错
注意:
我的数据库是8.0以上的版本,所以驱动使用的是com.mysql.cj.jdbc.Driver,而不是com.mysql.jdbc.Driver,url也要设置成useSSL=false
server:
port : 8081
spring:
datasource:
name : test
url : jdbc:mysql://127.0.0.1:3306/spring_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username : root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
这个时候是不是想问,如果同时存在properties和yml,项目会使用哪个?
答案是:properties,因为application.properties的优先级会比application.yml高,前提在同一目录层下。
2.2 mybatis配置文件
在resource目录创建mybatis-config.xml
内容如下:
<?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>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="true"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="3000"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<!-- Continue going here -->
</configuration>
同时在配置文件中引入mybatis-config.xml
mybatis:
config-location: classpath:mybatis-config.xml
三、创建映射器
3.1 创建Bean
我在数据库里创建用户表(t_user)
现在我们需要先创建一个user的bean
public class UserBean {
private String id;
private String name;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserBean{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
3.2 定义Dao接口
注意:使用**@Mapper**注解,才能容器里初始化
我这里使用的是注解的方法,但实际开发中应该尽可能使用xml的方式,虽然注解很方便,但对于复杂的sql语句不好维护(有关mybatis配置xml可以看——IDEA搭建mybatis框架DEMO)
@Mapper
public interface UserDao {
@Select("select id,name,password from t_user where id = #{id}")
UserBean getUserById(String id);
}
3.3 Controller
使用**@Autowired将UserDao注入,@RequestMapping**设置访问路径
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping(value = "/getData")
@ResponseBody
public String getUser(){
UserBean userBean = userDao.getUserById("admin");
return userBean.toString();
}
}
3.4 静态数据
在templates目录创建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
试试
</body>
</html>
创建HelloController,用于访问时跳转index页面
@Controller
public class HelloController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String Index(){
return "/index.html";
}
}
四、启动项目
选择DemoApplication,右键➡run
项目启动
五、测试
http://localhost:8081/hello:页面显示跳转到index.html
http://localhost:8081/user/getData :显示查询到的用户信息
更多推荐
所有评论(0)