打开并配置IDEA
IDEA更换工作空间:
(也可不更换,就用原来的)
关闭当前工作空间:
创建新项目/选择新的工作空间
然后会有弹框,x掉。
配置MAVEN
配置JDK:
模型自动创建springboot项目:(也可手动创建)
等待一会儿。
修改目录结构
引入依赖:(pom.xml)
```xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
```
配置文件:(application.xml)
```xml
server:
port: 8989
servlet:
context-path: /yingx
jsp:
init-parameters:
development: true
spring:
mvc:
view:
suffix: .jsp
prefix: /
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/yingx
username: root
password: 123456
mybatis:
type-aliases-package: com.baizhi.entity
mapper-locations: classpath:com/baizhi/mapper/*Mapper.xml
```
可以开始写代码了。
提示:先写个流程测试一下
举例:
entity/Admin.java
```java
package com.baizhi.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor //有参构造
@NoArgsConstructor //无参构造
public class Admin {
private String id;
private String username;
private String password;
}
```
resources/xxx/mapper/AdminMapper.xml
```java
select id,username,password from yx_admin where username=#{username}
```
dao/AdminDao.java
```java
package com.baizhi.dao;
import com.baizhi.entity.Admin;
public interface AdminDao {
/*
后台管理员登录方法
*/
public Admin queryByUsername(String username);
}
```
xxxApplication.java
```java
package com.baizhi;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.baizhi.dao")
@SpringBootApplication
public class YingxZhangykApplication {
public static void main(String[] args) {
SpringApplication.run(YingxZhangykApplication.class, args);
}
}
```
test/xxx.java
```java
package com.baizhi.yingx_zhangyk;
import com.baizhi.dao.AdminDao;
import com.baizhi.entity.Admin;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class YingxZhangykApplicationTests {
@Resource
AdminDao adminDao;
@Test
public void contextLoads() {
Admin admin= adminDao.queryByUsername("admin");
System.out.println(admin);
}
}
```
运行测试代码:
可以开始写项目了。