携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第13天,点击查看活动详情
一、java jdk下载
下载地址。我这边下载的是17:
二、Mac m1安装maven
下载安装地址。这里我下载的是3.8.5版本:
下载完之后配置.bash_profile
或者.zshrc
:
# maven
export MAVEN_HOME=/Users/justin/apache-maven-3.8.5
export PATH=$PATH:$MAVEN_HOME/bin
# maven end
路径换成你自己的路径。
测试:mvn -v
:
三、安装IntelliJ IDEA
下载安装IntelliJ IDEA
。选择适合自己电脑的版本安装即可。
四、配置IntelliJ IDEA中的maven版本
换成你本地安装的maven
。
五、创建maven项目
在pom.xml
中加入如下代码:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
</dependency>
</dependencies>
终端链接你本地mysql
服务:
mysql -uroot -p
并创建数据库tuling
:
create database tuling;
在tuling数据库下创建表:
create table t1(
a int not null,
b int not null,
c int not null,
d int not null,
e varchar(20) not null
) engine=innodb;
在resources
文件夹下新建application.properties
文件,写入:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tuling
spring.datasource.username=root
spring.datasource.password=你数据密码
在src\main\java
下新建文件夹com.justin
,新建文件如下:
UserMapper
写入代码:
package com.justin.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
@Insert("insert into t1 values(1,1,1,1,'1')")
void insertOne();
}
UserService
写入代码:
package com.justin.service;
import com.justin.mapper.UserMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Component
public class UserService {
@Resource
private UserMapper userMapper;
@Transactional
public void test() {
userMapper.insertOne();
throw new NullPointerException();
}
}
MyApplication
写入代码:
package com.justin;
import com.justin.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(MyApplication.class);
UserService userService = applicationContext.getBean(UserService.class);
userService.test();
}
}
点击运行
可以看到报错了,数据库也并没有插入任何数据。不过这个报错正是我们想要的,事务回滚了,我们接着把UserService
里的这行代码throw new NullPointerException();
注释掉:
运行:
可以看到正常,并且数据库也插入了数据:
六、spring搭建web工程
我们将pom.xml
的:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
的依赖改为:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如下图目录:
新建UserController
写入:
package com.justin.controller;
import com.justin.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("/test")
public void test() {
userService.test();
}
}
那么我们在MyApplication
中修改代码如下:
package com.justin;
import com.justin.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
在这里直接去运行就行了。
运行项目,我们在浏览器中访问http://localhost:8080/test
,就可以看到访问一次,数据库就插入一条数据:
在学习
springboot
的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。