美文网首页
SpringBoot 集成 Mybatis

SpringBoot 集成 Mybatis

作者: 奇梦人 | 来源:发表于2021-04-15 18:05 被阅读0次

    用 Interllij IDEA 创建项目就不贴了

    1. 添加相关依赖

    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.4</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jdbc</artifactId>
            </dependency>
            <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.4</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.29</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory><!--所在的目录-->
                    <includes><!--包括目录下的.properties,.xml文件都会扫描到-->
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <!--包含所在的目录的资源-->
                        <executable>true</executable>
                    </configuration>
                </plugin>
            </plugins>
    
    
        </build>
    
    </project>
    
    
    1. 编写 mapper

    LoginMapper

    @Mapper
    public interface LoginMapper {
        @Select("SELECT * FROM user WHERE id = #{id}")
        User selectUser(int id);
    
        User selectUserOnly(Long id);
    }
    

    LoginMapper.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.demo.mapper.LoginMapper">
    
        <select id="selectUserOnly" parameterType="Long" resultType="com.example.demo.bean.User">
            SELECT * FROM user WHERE id = #{id}
        </select>
    
    </mapper>
    
    1. 编写 Controller
    @RestController
    public class UserControl {
    
        @Autowired
        LoginMapper loginMapper;
    
        @RequestMapping(value = "/user", method = RequestMethod.GET)
        public User addNewArticle(@RequestParam(value = "user_id", defaultValue = "1")Integer id) {
            User user = loginMapper.selectUser(id);
            return user;
        }
    
        @RequestMapping(value = "/user_only", method = RequestMethod.GET)
        public User selectUserOnly(@RequestParam(value = "user_id", defaultValue = "1")Long id) {
            User user = loginMapper.selectUserOnly(id);
            return user;
        }
    }
    
    
    1. 配置文件
      application.properties
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    #spring.datasource.url=jdbc:mysql://120.79.214.74/vueblog2?useUnicode=true&characterEncoding=UTF-8
    spring.datasource.url=jdbc:mysql:///vueblog2?useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=123456
    mybatis.config-location=classpath:/mybatis-config.xml
    #jdbc:mysql://localhost:3306/heima
    server.port=8088
    logging.level.org.springframework.security=info
    
    1. 文件目录
    image.png

    相关文章

      网友评论

          本文标题:SpringBoot 集成 Mybatis

          本文链接:https://www.haomeiwen.com/subject/vvcjlltx.html