总体步骤按照如下:
第二步——修改pom文件:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_boot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project>
也可以按照教程上面的写,格式如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_boot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project>
第二步——配置数据源:
application.properties:
spring.main.banner-mode = off test.environment = read config by environment obj.sname=chenheng obj.sage=88 #设定日志的默认级别为info logging.level.root=info ##设定org包下的日志级别为warn #logging.level.org=warn ##设定com.ch.ch4_1包下的日志级别为debug #logging.level.com.ch.ch4_1=debug logging.file=c:/log/my.log logging.pattern.console=%level %date{yyyy-MM-dd HH:mm:ss:SSS} %logger{50}.%M %L :%m%n logging.pattern.file=%level %date{ISO8601} %logger{50}.%M %L :%m%n ### ##数据源信息配置 ### #数据库地址 spring.datasource.url=jdbc:mysql://localhost:3306/mysql8?characterEncoding=utf8 #数据库用户名 spring.datasource.username=root #数据库密码 spring.datasource.password=123456 #数据库驱动 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #告诉系统在哪里去找mapper.xml文件(映射文件) mybatis.mapperLocations=classpath:mappers/*.xml
第四步——创建实体类:
package org.example.entity; public class MyUser { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
第五步——创建数据库访问接口:
package org.example.mapper; import org.example.entity.MyUser; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { public List<MyUser> findAll(); }
第六步——创建mapper映射文件:
<?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="org.example.mapper.UserMapper"> <select id="findAll" resultType="org.example.entity.MyUser"> select * from users </select> </mapper>
第七步——创建业务层:
package org.example.service; import org.example.entity.MyUser; import java.util.List; public interface UserService { List<MyUser> findAll(); }
package org.example.service.Impl; import org.example.entity.MyUser; import org.example.mapper.UserMapper; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public List<MyUser> findAll() { return userMapper.findAll(); } }
第八步——创建控制器类:
package org.example.controller; import java.util.List; import org.example.entity.MyUser; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyUserController { @Autowired private UserService userService; @GetMapping(value = "/1") public List<MyUser> home1() { // 查询所有网站 List<MyUser> users = userService.findAll(); for (MyUser user : users) { System.out.println(user.getId()+" "+user.getName()+" "+user.getName()); } return users; } }
第九步——运行主类:
package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class helloWorldApplication { public static void main(String[] args) { SpringApplication.run(helloWorldApplication.class, args); } }
第十步——访问:
数据库表信息:
CREATE TABLE `users` ( `id` int NOT NULL, `name` varchar(255) DEFAULT NULL, `age` int DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
标签:spring,boot,example,org,mybatis,import,public From: https://www.cnblogs.com/xiaobaibailongma/p/17019676.html