赞
踩
命令
$ docker pull mysql
$ docker run --name local-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
相关地址
下载地址:
maven配置方法地址:
仓库搜索地址:
maven
本地配置conf/settings.xml
<!-- TAG··· --> <!-- 指定下载依赖到本地的路径 --> <localRepository>${user.home}/Documents/AAA-PLee/maven/repository</localRepository> <!-- TAG··· --> <!-- 配置为阿里云公共仓库 --> <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> <!-- TAG··· -->
下载idea
并配置本地环境maven
Maven
构建生命周期
Maven
的构建生命周期
包括三个阶段
:clean
、build
和site
clean
:清理项目,删除之前的编译结果和构建产生的文件。build
:构建项目,包括编译、测试、打包等操作。site
:生成项目文档和报告,例如测试报告、代码覆盖率报告等。Maven
会按照预定义的顺序依次执行。例如,在build
阶段,Maven
会依次执行以下目标:
validate
:验证项目是否正确。compile
:编译项目源代码。test
:运行项目的测试用例。package
:将项目打包成jar或war文件。verify
:验证打包结果是否正确。install
:将打包结果安装到本地Maven仓库。deploy
:将打包结果部署到远程Maven仓库。idea
所需插件下载EasyCode
用于快速生成与数据库相关的项目目录和代码
MyBatisPlus
用于
java
与sql.xml
之间的跳转
idea
创建项目Server URL
地址:https://start.aliyun.com/
idea
测试能否正常连接MySQL
MySQL
的配置MySQL
使用的依赖包在pom.xml
中引入MySQL依赖包
(别忘了Load Maven Changes
)
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
创建并编写application.yml
路径:
user/src/main/resources/application.yml
server:
port: 8000
spring:
application:
name: user-service
datasource:
url: jdbc:mysql://127.0.0.1:3306/java_app
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
EasyCode
快速创建操作数据库相关代码Java
应用程序通常使用以下几个组件来组织代码
这些组件之间的关系通常是
Controller
调用Service
,Service
定义impl
文件夹内的业务实现类
,业务实现类
调用DAO
,DAO
操作Mapper
中的SQL语句
,Entity
表示数据库表结构实体类
简单来说,
Controller
用于接收用户请求,Service
实现业务逻辑,DAO
操作数据库,Entity
表示数据库表结构
在
/service/impl/xxx.java
中实现具体业务逻辑在
/resources/mapper/xxx.xml
中添加操作数据的sql
语句
Controller
:控制器,用于接收用户请求并调用相应的处理方法来处理请求,然后返回响应结果。通常使用 Spring MVC
框架来实现。Service
:服务层,用于实现业务逻辑,调用 DAO
进行数据操作。通常包含接口和实现类。DAO
:数据访问对象,用于操作数据库。通常使用 MyBatis
、Hibernate
等框架来实现。Entity
:实体类,用于表示数据库中的表结构。通常包含类属性和对应的 getter/setter
方法。利用 https://mvnrepository.com/
搜索解决
<!-- 解决完成后的pom.xml --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
尝试运行(报错:没有引入对应版本)
<!-- <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.1.0</version>
</dependency> -->
<!-- 修改为 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
尝试运行(缺少 @MapperScan
注解)
注意:
尽量选择使用量大的依赖包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
// user/src/main/java/com/example/user/UserApplication.java
package com.example.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.example.user.dao")
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
@Configuration
,@EnableAutoConfiguration
和 @ComponentScan
尝试运行(运行成功,若运行后立即结束,有可能为未引入spring-boot-starter-web
包)
尝试引入即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
解决报错(告诉MyBatis
去哪里找SQL
语句)
mybatis:
mapper-locations: classpath:**/mapper/*.xml
user/pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>user</artifactId> <version>0.0.1-SNAPSHOT</version> <name>user</name> <description>user</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.6.13</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <mainClass>com.example.user.UserApplication</mainClass> <skip>true</skip> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
user/src/main/resources/application.yml
server:
port: 8000
spring:
application:
name: user-service
datasource:
url: jdbc:mysql://127.0.0.1:3306/java_app
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:**/mapper/*.xml
user/src/main/java/com/example/user/UserApplication.java
package com.example.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.example.user.dao")
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
user/src/main/java/com/example/user/controller/UserController.java
package com.example.user.controller; import com.example.user.entity.User; import com.example.user.service.UserService; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; /** * (User)表控制层 * * @author makejava * @since 2023-06-12 14:13:44 */ @RestController @RequestMapping("user") public class UserController { /** * 服务对象 */ @Resource private UserService userService; /** * 分页查询 * * @param user 筛选条件 * @param pageRequest 分页对象 * @return 查询结果 */ @GetMapping public ResponseEntity<Page<User>> queryByPage(User user, PageRequest pageRequest) { return ResponseEntity.ok(this.userService.queryByPage(user, pageRequest)); } /** * 通过主键查询单条数据 * * @param id 主键 * @return 单条数据 */ @GetMapping("{id}") public ResponseEntity<User> queryById(@PathVariable("id") String id) { return ResponseEntity.ok(this.userService.queryById(id)); } /** * 新增数据 * * @param user 实体 * @return 新增结果 */ @PostMapping public ResponseEntity<User> add(User user) { return ResponseEntity.ok(this.userService.insert(user)); } /** * 编辑数据 * * @param user 实体 * @return 编辑结果 */ @PutMapping public ResponseEntity<User> edit(User user) { return ResponseEntity.ok(this.userService.update(user)); } /** * 删除数据 * * @param id 主键 * @return 删除是否成功 */ @DeleteMapping public ResponseEntity<Boolean> deleteById(String id) { return ResponseEntity.ok(this.userService.deleteById(id)); } }
user/src/main/java/com/example/user/service/UserService.java
package com.example.user.service; import com.example.user.entity.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; /** * (User)表服务接口 * * @author makejava * @since 2023-06-12 14:13:49 */ public interface UserService { /** * 通过ID查询单条数据 * * @param id 主键 * @return 实例对象 */ User queryById(String id); /** * 分页查询 * * @param user 筛选条件 * @param pageRequest 分页对象 * @return 查询结果 */ Page<User> queryByPage(User user, PageRequest pageRequest); /** * 新增数据 * * @param user 实例对象 * @return 实例对象 */ User insert(User user); /** * 修改数据 * * @param user 实例对象 * @return 实例对象 */ User update(User user); /** * 通过主键删除数据 * * @param id 主键 * @return 是否成功 */ boolean deleteById(String id); }
user/src/main/java/com/example/user/service/impl/UserServiceImpl.java
package com.example.user.service.impl; import com.example.user.entity.User; import com.example.user.dao.UserDao; import com.example.user.service.UserService; import org.springframework.stereotype.Service; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import javax.annotation.Resource; /** * (User)表服务实现类 * * @author makejava * @since 2023-06-12 14:13:50 */ @Service("userService") public class UserServiceImpl implements UserService { @Resource private UserDao userDao; /** * 通过ID查询单条数据 * * @param id 主键 * @return 实例对象 */ @Override public User queryById(String id) { return this.userDao.queryById(id); } /** * 分页查询 * * @param user 筛选条件 * @param pageRequest 分页对象 * @return 查询结果 */ @Override public Page<User> queryByPage(User user, PageRequest pageRequest) { long total = this.userDao.count(user); return new PageImpl<>(this.userDao.queryAllByLimit(user, pageRequest), pageRequest, total); } /** * 新增数据 * * @param user 实例对象 * @return 实例对象 */ @Override public User insert(User user) { this.userDao.insert(user); return user; } /** * 修改数据 * * @param user 实例对象 * @return 实例对象 */ @Override public User update(User user) { this.userDao.update(user); return this.queryById(user.getId()); } /** * 通过主键删除数据 * * @param id 主键 * @return 是否成功 */ @Override public boolean deleteById(String id) { return this.userDao.deleteById(id) > 0; } }
user/src/main/java/com/example/user/dao/UserDao.java
package com.example.user.dao; import com.example.user.entity.User; import org.apache.ibatis.annotations.Param; import org.springframework.data.domain.Pageable; import java.util.List; /** * (User)表数据库访问层 * * @author makejava * @since 2023-06-12 14:13:45 */ public interface UserDao { /** * 通过ID查询单条数据 * * @param id 主键 * @return 实例对象 */ User queryById(String id); /** * 查询指定行数据 * * @param user 查询条件 * @param pageable 分页对象 * @return 对象列表 */ List<User> queryAllByLimit(User user, @Param("pageable") Pageable pageable); /** * 统计总行数 * * @param user 查询条件 * @return 总行数 */ long count(User user); /** * 新增数据 * * @param user 实例对象 * @return 影响行数 */ int insert(User user); /** * 批量新增数据(MyBatis原生foreach方法) * * @param entities List<User> 实例对象列表 * @return 影响行数 */ int insertBatch(@Param("entities") List<User> entities); /** * 批量新增或按主键更新数据(MyBatis原生foreach方法) * * @param entities List<User> 实例对象列表 * @return 影响行数 * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参 */ int insertOrUpdateBatch(@Param("entities") List<User> entities); /** * 修改数据 * * @param user 实例对象 * @return 影响行数 */ int update(User user); /** * 通过主键删除数据 * * @param id 主键 * @return 影响行数 */ int deleteById(String id); }
user/src/main/java/com/example/user/entity/User.java
package com.example.user.entity; import java.io.Serializable; /** * (User)实体类 * * @author makejava * @since 2023-06-12 14:13:46 */ public class User implements Serializable { private static final long serialVersionUID = 264722085318530649L; private String id; private String name; private Integer age; private String sex; private String phone; 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
user/src/main/resources/mapper/UserDao.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.user.dao.UserDao"> <resultMap type="com.example.user.entity.User" id="UserMap"> <result property="id" column="id" jdbcType="VARCHAR"/> <result property="name" column="name" jdbcType="VARCHAR"/> <result property="age" column="age" jdbcType="INTEGER"/> <result property="sex" column="sex" jdbcType="VARCHAR"/> <result property="phone" column="phone" jdbcType="VARCHAR"/> </resultMap> <!--查询单个--> <select id="queryById" resultMap="UserMap"> select id, name, age, sex, phone from user where id = #{id} </select> <!--查询指定行数据--> <select id="queryAllByLimit" resultMap="UserMap"> select id, name, age, sex, phone from user <where> <if test="id != null and id != ''"> and id = #{id} </if> <if test="name != null and name != ''"> and name = #{name} </if> <if test="age != null"> and age = #{age} </if> <if test="sex != null and sex != ''"> and sex = #{sex} </if> <if test="phone != null and phone != ''"> and phone = #{phone} </if> </where> limit #{pageable.offset}, #{pageable.pageSize} </select> <!--统计总行数--> <select id="count" resultType="java.lang.Long"> select count(1) from user <where> <if test="id != null and id != ''"> and id = #{id} </if> <if test="name != null and name != ''"> and name = #{name} </if> <if test="age != null"> and age = #{age} </if> <if test="sex != null and sex != ''"> and sex = #{sex} </if> <if test="phone != null and phone != ''"> and phone = #{phone} </if> </where> </select> <!--新增所有列--> <insert id="insert" keyProperty="id" useGeneratedKeys="true"> insert into user(name, age, sex, phone) values (#{name}, #{age}, #{sex}, #{phone}) </insert> <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true"> insert into user(name, age, sex, phone) values <foreach collection="entities" item="entity" separator=","> (#{entity.name}, #{entity.age}, #{entity.sex}, #{entity.phone}) </foreach> </insert> <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true"> insert into user(name, age, sex, phone) values <foreach collection="entities" item="entity" separator=","> (#{entity.name}, #{entity.age}, #{entity.sex}, #{entity.phone}) </foreach> on duplicate key update name = values(name), age = values(age), sex = values(sex), phone = values(phone) </insert> <!--通过主键修改数据--> <update id="update"> update user <set> <if test="name != null and name != ''"> name = #{name}, </if> <if test="age != null"> age = #{age}, </if> <if test="sex != null and sex != ''"> sex = #{sex}, </if> <if test="phone != null and phone != ''"> phone = #{phone}, </if> </set> where id = #{id} </update> <!--通过主键删除--> <delete id="deleteById"> delete from user where id = #{id} </delete> </mapper>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。