Fork me on GitHub

SpringBoot———整合Mybatis-xml方式

SpringBoot———整合Mybatis-xml方式

1.创建Maven工程

1.1pom.xml
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<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>
<parent>
<groupId>net.zzqd</groupId>
<artifactId>SpringBoot_Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringBoot-Child1</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>

<!-- 加入依赖,代码做修改,可以不用重新运行 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<!-- springboot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!-- mybatis的分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</project>
1.2mybatis-config.xml
1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>
1.3全局配置文件(applicatiton.properties)
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
27
28
29
30
31
32
33
34
35
36
spring:
datasource:
name: test
url: jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatement: 20



mybatis:
mapper-location: classpath:mapping/CustomerMapper.xml
#type-aliases-package: net.zzqd.pojo
config-location: classpath:mybatis/mybatis-config.xml



pagehelper:
helperDialect: mysql
reasonable: true
supprotMethodsArguments: true
params: count=countSql
1.4pojo以及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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//pojo
package net.zzqd.pojo;

public class Customer {
private Integer id;
private String name;
private String gender;
private String telephone;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//mapper
package net.zzqd.mapper;

import java.util.List;

import net.zzqd.pojo.Customer;

public interface CustomerMapper
{
/**
* 查询所有数据
*/
public List<Customer> findAll();

/**
* 保存数据
* @param customer
*/
public void save(Customer customer);
/**
* 根据id查询对象
* @param id
* @return
*/
public Customer findById(Integer id);

/**
* 修改对象数据
* @param customer
*/
public void update(Customer customer);

/**
* 删除数据
* @param id
*/
public void delete(Integer[] id);


}
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//mapper.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="net.zzqd.mapper.CustomerMapper">

<!-- 查询所有数据 用resultType封装-->
<select id="findAll" resultType="net.zzqd.pojo.Customer">
select id,
name,
gender,
telephone,
address
from
ssm.t_customer

</select>
<!-- 添加客户 -->
<insert id="save" parameterType="net.zzqd.pojo.Customer">
insert into ssm.t_customer
(name,gender,telephone,address)
values
(
#{name},
#{gender},
#{telephone},
#{address}
)

</insert>
<!-- 根据id查询对象 -->
<select id="findById" parameterType="int" resultType="net.zzqd.pojo.Customer">
select id,
name,
gender,
telephone,
address
from
ssm.t_customer
where id=#{value}
</select>
<!-- 根据id修改数据 -->
<update id="update" parameterType="net.zzqd.pojo.Customer">
update ssm.t_customer set
name=#{name},
gender=#{gender},
telephone=#{telephone},
address=#{address}
where
id=#{id}
</update>
<!-- 根据id删除用户 -->
<delete id="delete" parameterType="Integer[]">
delete from ssm.t_customer
<where>
id
<foreach collection="array" item="id" open="in (" close=")" separator=",">
#{id}
</foreach>
</where>
</delete>
</mapper>

2.业务层代码

1
2
3
4
5
6
7
8
9
10
11
12
13
package net.zzqd.service;

import java.util.List;

import net.zzqd.pojo.Customer;

public interface CustomerService {
//添加用户
void addCustomer(Customer customer);

//分页查找
List<Customer> findCustomer(int page,int rows);
}
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
27
28
29
30
31
32
33
package net.zzqd.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import net.zzqd.mapper.CustomerMapper;
import net.zzqd.pojo.Customer;

@Service
public class CustomerServiceImpl implements CustomerService {

@Autowired
private CustomerMapper customerMapper;

@Override
public void addCustomer(Customer customer) {

customerMapper.save(customer);
}

@Override
public List<Customer> findCustomer(int page, int rows) {
List<Customer> customers = customerMapper.findAll();
PageHelper.startPage(page, rows);

return customers;
}

}

3.控制层代码

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
27
28
29
30
31
32
33
34
35
36
37
38
39
package net.zzqd.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.zzqd.pojo.Customer;
import net.zzqd.service.CustomerService;

@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;

@RequestMapping("/saveCustomer")
@ResponseBody
public String saveCustomer()
{
Customer customer = new Customer();
customer.setName("张张");
customer.setGender("女");
customer.setAddress("郑州");
customer.setTelephone("18888885284");

customerService.addCustomer(customer);
return "success";
}

@RequestMapping("/findCustomer/{page}/{rows}")
@ResponseBody
public List<Customer> findCustomer(@PathVariable int page, @PathVariable int rows)
{
return customerService.findCustomer(page, rows);
}
}

4.运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package net.zzqd.app;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

//@EnableAutoConfiguration
//@ComponentScan("net.zzqd.controller")
//默认情况下扫描的是当前包以及当前包的子包
@SpringBootApplication(scanBasePackages= {"net.zzqd"})//组合注解
@MapperScan("net.zzqd.mapper")
public class SpringApplications {
public static void main(String[] args)
{
SpringApplication.run(SpringApplications.class, args);
}
}

附加:遇到异常:Cannot determine embedded database driver class for database type NONE

暂时未解决

修改后异常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘customerController’: Unsatisfied dependency expressed through field ‘customerService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘customerServiceImpl’: Unsatisfied dependency expressed through field ‘customerMapper’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘customerMapper’ defined in file [C:\DevelopTools\JavaFramework\SpringBoot_Parent\SpringBoot-Child10\target\classes\net\zzqd\mapper\CustomerMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required