Home > Java > javaTutorial > How to integrate mybatis xml in springboot

How to integrate mybatis xml in springboot

PHPz
Release: 2023-05-10 21:43:04
forward
1392 people have browsed it

springboot integrates mybatis

1, add pom reference

 <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>
Copy after login

2 application.properties

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
Copy after login

3 in resource Create the mybatis directory under the directory and create the mybatis-config.xml file

<?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>
Copy after login

Create the mapper directory under the mybatis directory to store the mapper class interface file

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);
 }
Copy after login

model class file

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);
}
Copy after login

userMapper.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.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>
Copy after login

controller:

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);
  }  
}
Copy after login

Finally, add the scanning maper interface annotation

@SpringBootApplication
@MapperScan("com.kerry.mapper")
public class Application {
 
 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 }
}
Copy after login

or in each It is also possible to add the @mapper annotation to an XXMapper class. You can choose one of the two. It is not necessary to add the @mapper annotation every time you write a mapper class

Attach the selected classes and files in the project structure directory

The above is the detailed content of How to integrate mybatis xml in springboot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template