springboot整合mybatis

1.增删改查“

添加mysql-connector和mysql-spring-boot-start依赖:

  <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

2.配置数据库,在application.yml中:

#配置数据源
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

3.构建一个实体类:

public class User {
    private Long id;
    private String city;
    private Integer num;

//get,set方法
}

4.书写dao层,在类中写sql语句:

@Mapper
@Repository
public interface UserDao {
  @Select("select * from user where city=#{city}")
    User findUserByCity(@Param("city") String city);
  @Insert("insert into user(city, num) VALUES(#{city}, #{num})")
  int insertUser(@Param("city") String city, @Param("num") Integer num);
}

5.书写我们的持久化操作的业务逻辑services,并调用dao层完成持久化的操作

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public User selectService(String city){
        return (User) userDao.findUserByCity(city);
    }

    @Transactional
    public void insertService(){
        userDao.insertUser("西安", 9421);
        userDao.insertUser("长安", 1294);
    }
}

6.完成我们的视图层的代码编写,定义类的访问路径,方法的访问路经,以及访问后返回的结果

    @RestController
   // @RequestMapping("/demo")
    public class UserController {
        @Autowired
        private UserService userService;

        @RequestMapping("/query")
        public User testQuery(){
            return (User) userService.selectService("北京");
        }

        @RequestMapping("/insert")
        public String testInsert(){
            userService.insertService();
            return "ok";
          //  return (User) userService.selectService("西安");
        }
    }

大功告成

下面对比注解开发,说一下xml形式的使用:

2.1首先,需要在application.yml中添加mybatis映射文件的地址(即书写sql的xxx.xml在哪,以便spring能把它扫进来)

#指定mybatis映射文件的地址
mybatis:
  mapper-locations: classpath:mapper/*.xml

2.2在xxx.xml中书写上sql语句(即你对数据库的骚操作):注意namespape命名空间是xxxDao的命名空间,结果集是相应的实体类

<?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="my.blog.dao.UserDao">
    <select id="findUserByCity" parameterType="String" resultType="my.blog.domain.User">
        SELECT * FROM user WHERE city = #{city}
    </select>

    <insert id="insertUser" parameterType="HashMap" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO user(city, num) VALUES(#{city}, #{num})
    </insert>
</mapper>

2.3应为sql语句已经和类分离了,故只需在Dao层的接口中说明其对应的方法就可以

@Mapper
@Repository
public interface UserDao {
  User findUserByCity(String city);

  int insertUser(HashMap<String, Object> map);
}

2.4service只需更改一下insert的代码就可以了,因为我们用到了hashmap

@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    public User selectService(String city){
        return (User) userDao.findUserByCity(city);
    }

    @Transactional
    public void insertService(){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("city", "西安");
        map.put("num", 9421);
    }
}

2.5:Controller层的代码不用变

 

posted @ 2018-10-28 05:24  goodshred  阅读(193)  评论(0编辑  收藏  举报