SpringMVC - 从零接入 MyBatis

继续后端服务系列:

从零接入 MyBatis,网上例子很多,这里推荐一份仅供参考,该例子中自带源码。
(mapper 等代码其实可以自动生成,后续再讲)

https://how2j.cn/k/ssm/ssm-tutorial/1137.html

1. 准备数据库/数据

2. 依赖包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>

<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>

3. pojo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.tracenote.pojo;

public class Category {
private int id;
private String name;
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;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}

4. mapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.tracenote.mapper;


import com.tracenote.pojo.Category;
import java.util.List;

public interface CategoryMapper {

public int add(Category category);


public void delete(int id);


public Category get(int id);


public int update(Category category);


public List<Category> list();


public int count();

}

5. mapper.xml

注意 Intellij IEDA 需要放入 resources 目录下(除非 pom.xml 追加 build 配置包含 java 目录)。(作者源码示例目录是eclipse的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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.tracenote.mapper.CategoryMapper">
<insert id="add" parameterType="com.tracenote.pojo.Category" >
insert into category_ ( name ) values (#{name})
</insert>

<delete id="delete" parameterType="com.tracenote.pojo.Category" >
delete from category_ where id= #{id}
</delete>

<select id="get" parameterType="_int" resultType="com.tracenote.pojo.Category">
select * from category_ where id= #{id}
</select>

<update id="update" parameterType="com.tracenote.pojo.Category" >
update category_ set name=#{name} where id=#{id}
</update>
<select id="list" resultType="com.tracenote.pojo.Category">
select * from category_
</select>
</mapper>

6. mybatisContext.xml

放入 resources 目录下,确保 spring 可以扫到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/app_auto?characterEncoding=UTF-8</value>

</property>
<property name="username">
<value>tracenote</value>
</property>
<property name="password">
<value>tracenote</value>
</property>
</bean>


<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.tracenote.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tracenote.mapper"/>
</bean>

7. 使用

如果配置无误,且没有其它版本冲突之类,就可以在程序中使用了。

1
2
3
4
5
6
@Autowired
private CategoryService categoryService;

// ...

List<Category> cs= categoryService.list();