• (二十)SpringBoot之集成mybatis:使用mybatis注解


    • 一、使用mybatis注解的集成

      1.1  引入maven依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.2.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>

      1.2  配置application.properties

    # mysql
    spring.datasource.url=jdbc:mysql://localhost/db_boot?useUnicode=true&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    logging.level.com.shyroke.mapper=debug
    • logging.level.com.shyroke.mapper=debug表示打印出sql语句 logging.level.mapper所在包

      1.3  实体类

    package com.shyroke.bean;
    
    import java.io.Serializable;
    import java.util.Date;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    public class UserBean implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        private Integer id;
    
        private String userName;
    
        private String passWord;
    
        private Date createTime;
    
        //省略getset和toString方法
    
    }

      1.4  编写控制器

    package com.shyroke.controller;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.shyroke.bean.UserBean;
    import com.shyroke.mapper.UserMapper;
    
    @Controller
    @RequestMapping(value = "/")
    public class UserController {
    
        @Autowired
        private UserMapper userMapper;
        
        @RequestMapping(value="/users")
        @ResponseBody
        public List<UserBean> getUsers() {
    
            List<UserBean> userList = userMapper.getUsers();
    
            return userList;
    
        }
    
        @ResponseBody
        @RequestMapping(value="/update")
        public List<UserBean> update() {
            UserBean user=new UserBean();
            user.setId(1);
            user.setUserName("updateName");
            user.setPassWord("123");
            user.setCreateTime(new Date());
            userMapper.update(user);
            return userMapper.getUsers();
            
        }
        
        @ResponseBody
        @RequestMapping(value="/del")
        public List<UserBean> del(){
            
            
            userMapper.del(1);
            
            return userMapper.getUsers();
        }
        
        @ResponseBody
        @RequestMapping(value="add")
        public List<UserBean> add(){
            
            UserBean user=new UserBean();
            user.setUserName("test");
            user.setPassWord("111");
            user.setCreateTime(new Date());
            
            userMapper.save(user);
            return userMapper.getUsers();
        }
        
    }

      1.5  编写mapper

    package com.shyroke.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Options;
    import org.apache.ibatis.annotations.Result;
    import org.apache.ibatis.annotations.Results;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    import org.apache.ibatis.type.JdbcType;
    
    import com.shyroke.bean.UserBean;
    
    @Mapper
    public interface UserMapper {
    
        /**
         * 获取所有用户
         * 
         * @return
         */
        @Select(value = "select * from users")
        @Results(value = { @Result(column = "user_name", property = "userName", jdbcType = JdbcType.VARCHAR),
                @Result(column = "pass_word", property = "passWord", jdbcType = JdbcType.VARCHAR),
                @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATE) })
        List<UserBean> getUsers();
    
        /**
         * 修改用户信息
         * 
         * @param user
         */
        @Update("update users set user_name= #{userName},pass_word=#{passWord},create_time=#{createTime} where id=#{id}")
        void update(UserBean user);
    
        /**
         * 删除用户
         * 
         * @param id用户id
         */
        @Delete("delete from users where id=#{id}")
        void del(int id);
    
        /**
         * 新增一条用户信息
         * 
         * @param user
         */
        @Insert("insert into users(user_name,pass_word,create_time) values(#{userName},#{passWord},#{createTime})")
        void save(UserBean user);
    
    }

      1.6  结果

     

  • 相关阅读:
    unzip解压3G或者4G以上文件失败的解决方法
    zencart批量删除无图片产品
    zencart后台管理中选项名称和选项内容和属性控制页面出错解决办法 WARNING: An Error occurred, please refresh the page and try again
    在线随机密码生成工具
    zencart更改css按钮的宽度css buttons
    IE8"HTML Parsing Error:Unable to modify the parent container element before the child element is closed"错误
    css改变背景透明度
    phpMyAdmin出现Fatal error: Maximum execution time of 300 seconds
    da面板修改SSH端口号
    原生js三级联动
  • 原文地址:https://www.cnblogs.com/shyroke/p/8040137.html
Copyright © 2020-2023  润新知