详解springboot集成mybatis xml方式
程序员文章站
2024-02-13 19:59:04
springboot集成mybatis
关键代码如下:
1,添加pom引用
org...
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 快速整合Mybatis(去XML化+注解进阶)
-
创建SpringBoot工程并集成Mybatis的方法
-
创建SpringBoot工程并集成Mybatis的方法
-
springboot与mybatis整合实例详解
-
springboot 注册服务注册中心(zk)的两种方式详解
-
SpringBoot框架与MyBatis集成,连接Mysql数据库
-
SpringBoot整合MyBatis-Plus3.1教程详解
-
详解mybatis的mapper.xml编写 博客分类: mybatis javamybatis
-
apollo与springboot集成实现动态刷新配置的教程详解