zoukankan      html  css  js  c++  java
  • Spring Boot 集成 MyBatis--跟着慕课熊猫学

    Spring Boot 集成 MyBatis

    企业级应用数据持久层框架,最常见的应该是 Hibernate 和 MyBatis 。

    Hibernate 是相当彻底的 ORM 对象 - 关系映射框架,使用 Hibernate ,开发者可以不考虑 SQL 语句的编写与执行,直接操作对象即可。

    与 Hibernate 相比, MyBatis 还是需要手工编写 SQL 语句的。

    除此之外,MyBatis 是更加简单,更容易上手的框架,但是功能也是相对简陋

    相关依赖

       <dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter</artifactId>
    	</dependency>
    	<!-- 热部署 -->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-devtools</artifactId>
    	</dependency>
    	<!-- Web支持 -->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-web</artifactId>
    	</dependency>
    	<!-- JDBC -->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-jdbc</artifactId>
    	</dependency>
    	<!-- MySQL驱动 -->
    	<dependency>
    		<groupId>mysql</groupId>
    		<artifactId>mysql-connector-java</artifactId>
    	</dependency>
    	<!-- 集成MyBatis -->
    	<dependency>
    		<groupId>org.mybatis.spring.boot</groupId>
    		<artifactId>mybatis-spring-boot-starter</artifactId>
    		<version>2.1.2</version>
    	</dependency>
    	<!-- junit -->
    	<dependency>
    		<groupId>junit</groupId>
    		<artifactId>junit</artifactId>
    		<scope>test</scope>
    	</dependency>
    	<!-- 测试 -->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-test</artifactId>
    		<scope>test</scope>
    		<exclusions>
    			<exclusion>
    				<groupId>org.junit.vintage</groupId>
    				<artifactId>junit-vintage-engine</artifactId>
    			</exclusion>
    		</exclusions>
    	</dependency>
    

    配置文件

    # 配置数据库驱动
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    # 配置数据库url
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    # 配置数据库用户名
    spring.datasource.username=root
    # 配置数据库密码
    spring.datasource.password=123456
    
    # 指定MyBatis配置文件位置
    mybatis.mapper-locations=classpath:mapper/*.xml
    

    数据对象类

    /**
     * 商品类
     */
    public class GoodsDo {
    	/**
    	 * 商品id
    	 */
    	private Long id;
    	/**
    	 * 商品名称
    	 */
    	private String name;
    	/**
    	 * 商品价格
    	 */
    	private String price;
    	/**
    	 * 商品图片
    	 */
    	private String pic;
    	// 省略 get set方法
    }
    

    开发数据访问层

    数据访问层直接使用接口实现即可,接口中添加商品的增删改查基本操作。

    实例:

    /**
     * 商品数据库访问接口
     */
    @Repository // 标注数据访问组件
    public interface GoodsDao {
    	/**
    	 * 新增商品
    	 */
    	public int insert(GoodsDo Goods);
    
    	/**
    	 * 删除商品(根据id)
    	 */
    	public int delete(Long id);
    
    	/**
    	 * 修改商品信息(根据id修改其他属性值)
    	 */
    	public int update(GoodsDo Goods);
    
    	/**
    	 * 查询商品信息(根据id查询单个商品信息)
    	 */
    	public GoodsDo selectOne(Long id);
    
    	/**
    	 * 查询商品列表
    	 */
    	public List<GoodsDo> selectAll();
    }
    

    启动文件

    @SpringBootApplication
    @MapperScan("com.imooc.springbootmybatis") // 指定MyBatis扫描的包,以便将数据访问接口注册为bean
    public class SpringBootMybatisApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(SpringBootMybatisApplication.class, args);
    	}
    }
    

    映射文件

    resources/mapper 目录下新建 GoodsMapper.xml 文件,该文件就是 goods 表对应的映射文件,内容如下:

    实例:

    <?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">
    <!-- 本映射文件对应GoodsDao接口 -->
    <mapper namespace="com.imooc.springbootmybatis.GoodsDao">
    	<!-- 对应GoodsDao中的insert方法 -->
    	<insert id="insert" parameterType="com.imooc.springbootmybatis.GoodsDo">
    		insert into goods (name,price,pic) values (#{name},#{price},#{pic})
    	</insert>
    	<!-- 对应GoodsDao中的delete方法 -->
    	<delete id="delete" parameterType="java.lang.Long">
    		delete from goods where id=#{id}
    	</delete>
    	<!-- 对应GoodsDao中的update方法 -->
    	<update id="update" parameterType="com.imooc.springbootmybatis.GoodsDo">
    		update goods set name=#{name},price=#{price},pic=#{pic} where id=#{id}
    	</update>
    	<!-- 对应GoodsDao中的selectOne方法 -->
    	<select id="selectOne" resultMap="resultMapBase" parameterType="java.lang.Long">
    		select <include refid="sqlBase" /> from goods where id = #{id}
    	</select>
    	<!-- 对应GoodsDao中的selectAll方法 -->
    	<select id="selectAll" resultMap="resultMapBase">
    		select <include refid="sqlBase" /> from goods
    	</select>
    	<!-- 可复用的sql模板 -->
    	<sql id="sqlBase">
    		id,name,price,pic
    	</sql>
    	<!-- 保存SQL语句查询结果与实体类属性的映射 -->
    	<resultMap id="resultMapBase" type="com.imooc.springbootmybatis.GoodsDo">
    		<id column="id" property="id" />
    		<result column="name" property="name" />
    		<result column="price" property="price" />
    		<result column="pic" property="pic" />
    	</resultMap>
    </mapper>
    

    测试

    /**
    	 * 新增一个商品
    	 */
    	@Test
    	void test_01() {
    		GoodsDo goods = new GoodsDo();
    		goods.setName("手机");
    		goods.setPic("phone.jpg");
    		goods.setPrice("2000");
    		int count = goodsDao.insert(goods);
    		assertEquals(1, count);// count值为1则测试通过
    	}
    
    由于无法解释的神圣旨意,我们徒然地到处找你;你就是孤独,你就是神秘,比恒河或者日落还要遥远。。。。。。
  • 相关阅读:
    mysql数据库常用指令
    解决windows的mysql无法启动 服务没有报告任何错误的经验。
    “Can't open file for writing”或“operation not permitted”的解决办法
    启动Apache出现错误Port 80 in use by "Unable to open process" with PID 4!
    如何打开windows的服务services.msc
    常见的HTTP状态码 404 500 301 200
    linux系统常用的重启、关机指令
    (wifi)wifi移植之命令行调试driver和supplicant
    linux(debian)安装USB无线网卡(tp-link TL-WN725N rtl8188eu )
    alloc_chrdev_region申请一个动态主设备号,并申请一系列次设备号
  • 原文地址:https://www.cnblogs.com/momoli/p/13853649.html
Copyright © 2011-2022 走看看