今天就跟大家聊聊有关如何在springboot中集成mybatis xml方式,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联公司专注于嫩江企业网站建设,响应式网站开发,商城网站建设。嫩江网站建设公司,为嫩江等地区提供建站服务。全流程定制网站,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务springboot集成mybatis
1,添加pom引用
<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
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文件
<?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类接口文件
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类文件
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
<?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:
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注解
@SpringBootApplication @MapperScan("com.kerry.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
或者在每个XXMapper类上加上@mapper注解也行 二选一即可
比如
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方式有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
网页题目:如何在springboot中集成mybatisxml方式-创新互联
本文路径:https://www.cdcxhl.com/article12/jehdc.html
成都网站建设公司_创新互联,为您提供品牌网站建设、微信公众号、网站导航、关键词优化、面包屑导航、ChatGPT
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联