详解springboot集成mybatis xml方式

时间:2022-11-14 20:26:32

springboot集成mybatis

关键代码如下:

1,添加pom引用

?
1
2
3
4
5
6
7
8
9
<dependency>
 <groupid>org.mybatis.spring.boot</groupid>
 <artifactid>mybatis-spring-boot-starter</artifactid>
 <version>1.1.1</version>
</dependency>
  <dependency>
    <groupid>mysql</groupid>
    <artifactid>mysql-connector-java</artifactid>
  </dependency>

2 application.properties

?
1
2
3
4
5
6
7
8
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.kerry.model
 
spring.datasource.driverclassname = com.mysql.jdbc.driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456

3 在resource目录下创建mybatis目录并创建mybatis-config.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
<?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>
 <settings>
 <setting name="callsettersonnulls" value="true"/>
 
 <setting name="cacheenabled" value="true"/>
 
 <setting name="lazyloadingenabled" value="true"/>
 
 <setting name="aggressivelazyloading" value="true"/>
 
 <setting name="multipleresultsetsenabled" value="true"/>
 
 <setting name="usecolumnlabel" value="true"/>
 
 <setting name="usegeneratedkeys" value="false"/>
 
 <setting name="automappingbehavior" value="partial"/>
 
 <setting name="defaultexecutortype" value="simple"/>
 
 <setting name="mapunderscoretocamelcase" value="true"/>
 
    <setting name="localcachescope" value="session"/>
 
    <setting name="jdbctypefornull" value="null"/>
 
 </settings>
 
 <typealiases>
 <typealias alias="integer" type="java.lang.integer" />
 <typealias alias="long" type="java.lang.long" />
 <typealias alias="hashmap" type="java.util.hashmap" />
 <typealias alias="linkedhashmap" type="java.util.linkedhashmap" />
 <typealias alias="arraylist" type="java.util.arraylist" />
 <typealias alias="linkedlist" type="java.util.linkedlist" />
 </typealiases>
</configuration>

mybatis目录下创建mapper目录存放mapper类接口文件

?
1
2
3
4
5
6
7
8
9
10
package com.kerry.mapper;
import java.util.list;
import com.kerry.model.user;
public interface usermapper {
 list<user> getall();
 user getone(integer id);
 void insert(user user);
 void update(user user);
 void delete(integer id);
 }

model类文件

?
1
2
3
4
5
6
7
8
9
10
package com.kerry.mapper;
import java.util.list;
import com.kerry.model.user;
public interface usermapper {
 list<user> getall();
 user getone(integer id);
 void insert(user user);
 void update(user user);
 void delete(integer id);
}

usermapper.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
<?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.kerry.mapper.usermapper" >
  <resultmap id="baseresultmap" type="com.kerry.model.user" >
    <id column="id" property="id" jdbctype="integer" />
    <result column="name" property="name" jdbctype="varchar" />
    <result column="age" property="age" jdbctype="varchar" />
    <result column="address" property="address" jdbctype="varchar"/>
  </resultmap>
  
  <sql id="base_column_list" >
    id, name, age, address
  </sql>
 
  <select id="getall" resultmap="baseresultmap" >
    select
    <include refid="base_column_list" />
  from user
  </select>
 
  <select id="getone" parametertype="java.lang.integer" resultmap="baseresultmap" >
    select
    <include refid="base_column_list" />
  from user
  where id = #{id}
  </select>
 
  <insert id="insert" parametertype="com.kerry.model.user" >
    insert into
     user
     (id,name,age,address)
    values
     (#{id},#{name}, #{age}, #{address})
  </insert>
  
  <update id="update" parametertype="com.kerry.model.user" >
    update
     user
    set
    <if test="name != null">name = #{name},</if>
    <if test="age != null">age = #{age},</if>
    address = #{address}
    where
     id = #{id}
  </update>
  
  <delete id="delete" parametertype="java.lang.integer" >
    delete from
     user
    where
     id =#{id}
  </delete>
</mapper>

controller:

?
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
package com.kerry.web;
import java.util.list;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import com.kerry.model.user;
import com.kerry.mapper.usermapper;
@restcontroller
public class usercontroller {
 @autowired
 private usermapper usermapper;
 
 @requestmapping("/getusers")
 public list<user> getusers() {
 list<user> users=usermapper.getall();
 return users;
 }
 
  @requestmapping("/getuser")
  public user getuser(integer id) {
   user user=usermapper.getone(id);
    return user;
  }
  
  @requestmapping("/add")
  public void save(user user) {
   usermapper.insert(user);
  }
  
  @requestmapping(value="update")
  public void update(user user) {
   usermapper.update(user);
  }
  
  @requestmapping(value="/delete/{id}")
  public void delete(@pathvariable("id") integer id) {
   usermapper.delete(id);
  
}

最后在启动类上加上 扫描maper interface注解

?
1
2
3
4
5
6
7
8
@springbootapplication
@mapperscan("com.kerry.mapper")
public class application {
 
 public static void main(string[] args) {
 springapplication.run(application.class, args);
 }
}

或者在每个xxmapper类上加上@mapper注解也行  二选一即可

比如

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.kerry.mapper;
import java.util.list;
import org.apache.ibatis.annotations.mapper;
import com.kerry.model.user;
 @mapper
public interface usermapper {
 list<user> getall();
 user getone(integer id);
 void insert(user user);
 void update(user user);
 void delete(integer id);
}

个人建议使用直接在启动类上application上@mapperscan方便,一次搞定,不必每写一个mapper类都加上@mapper注解

附上项目结构目录 选中的类以及文件

详解springboot集成mybatis xml方式

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/lr131425/article/details/76269236