Skip to content

MyBatis-Spring 入门

官方的文档里写的太简单了,这里是根据文档创建的示例程序。

使用 MyBatis-Spring 访问 SqlServer 数据库。

1. pom.xml 添加依赖

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>liujiajia.me.learning.mybatis</groupId>
    <artifactId>learning-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </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.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency>
    </dependencies>
</project>

2. 在 resources 目录中创建 applicationContext.xml 配置文件

配置 SqlSessionFactoryDataSourceMapper

xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--Sql Session Factory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--数据源-->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://192.168.0.1:1433;databaseName=TEST" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>
    <!--Mapper-->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="liujiajia.me.learning.mybatis.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
</beans>

3. Application.java

创建程序入口,并导入配置文件。

java
package liujiajia.me.learning.mybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

/**
* Created by liujiajia on 2019/1/23.
*/
@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4. UserMapper.java

表模型 User 根据自己的需要创建。

java
package liujiajia.me.learning.mybatis;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
* Created by liujiajia on 2019/1/23.
*/
public interface UserMapper {
    @Select("SELECT * FROM User WHERE Account = #{account}")
    User getUser(@Param("account") String account);
}

5. UserController.java

java
package liujiajia.me.learning.mybatis;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* Created by liujiajia on 2019/1/23.
*/
@RestController
@RequestMapping("api/user")
public class UserController {
    @Resource
    private UserMapper userMapper;

    @RequestMapping(value = "{account}", method = RequestMethod.GET)
    public User getUser(@PathVariable("account") String account) {
        return userMapper.getUser(account);
    }
}

6. 启动应用

启动 Application,然后访问 http://localhost:8080/api/user/account 查看是否能获取到数据。

SpringBoot 默认使用 8080 端口。

如需修改可通过启动参数 --server.port=8080 来自定义端口号。
或者通过在 resources 目录下创建 applicaton.yml 配置文件并设置如下内容来自定义端口号。

yaml
server:
    port: 8083

参数和 applicaton.yml 配置文件同时使用时,参数的优先级较高。

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.