springboot使用Mybatis的步骤

  • 创建一个maven-jar新项目
    项目结构springboot使用Mybatis的步骤_第1张图片
  • pom导入
    在pom.xml文件中加入
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>
    <dependencies>
    
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      
    <!-- 引入freeMarker的依赖包. -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    
    <!-- 引用mybatis的依赖包 -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>

    <!-- 引用mysql驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

  </dependencies>
  • 数据库配置文件
    在main下面生成resources文件夹并标记为resource root再在resources下面的生成application.properties文件。
#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  • mapper的编写
package com.fb.mapper;

import com.fb.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {
    //添加用户
    @Insert("insert user (name,password) values (#{name},#{password}")
    public int save(@Param("name") String name,@Param("password") String password);

    //查询用户
    @Select("select * from user where name = #{name}")
    public User findUserByName(@Param("name") String name);

}

  • service编写
    service上面可以加@Transaction,注释事务,但也可以省略,会自己添加事务的
    springboot使用Mybatis的步骤_第2张图片
package com.fb.service.impl;

import com.fb.mapper.UserMapper;
import com.fb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service//声明service
@Transaction//声明事务
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(String name, String password) {
        userMapper.save(name,password);
    }
}

  • Controller的编写

@RestController 注解:告诉 Spring 以字符串的形式渲染结果,并直接返回给调用者。该注解相当于 @ResponseBody + @Controller 合在一起的作用。
@RequestMapping 注解:提供路由信息,它告诉 Spring 任何来自“/hello”路径的 HTTP 请求(Get)都应该被映射到 hello 方法。

- @RequestMapping和@GetMapping区别

@RequestMapping可以指定GET、POST请求方式
@GetMapping等价于@RequestMapping的GET请求方式
使用springboot搭建springmvc测试如下:

package com.fb.web.controller;

import com.fb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController //声明Rest风格的控制器//声明控制器
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("register")
    @ResponseBody
    public String register(String name,String password){

        userService.register(name,password);
        return "success";
    }
}

  • App编写
    用注释声明要解析的包
    @MapperScan(basePackages = “com.fb.mapper”)
    @ComponentScan(basePackages = {“com.fb.web.controller”,“com.fb.service”})
package com.fb;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

/**
 * Hello world!
 *
 */
@EnableAutoConfiguration
@MapperScan(basePackages = "com.fb.mapper")
@ComponentScan(basePackages = {"com.fb.web.controller","com.fb.service"})
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class,args);
    }
}

  • 运行项目

访问http://localhost:8080/user/register?name=fb&password=1234
如果mapper换成xml形式不使用注解,那么添加一个mapper的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.fb.mapper.UserMapper" >

    <insert id="save">
        insert into t_user (username,password) VALUES(#{0},#{1})
    </insert>
    <select id="findByUsername" resultType="com.gyf.model.User" parameterType="string">
        select * from t_user where username = #{username,jdbcType=VARCHAR}
    </select>
</mapper>

注意这里就需要在pom中添加下面代码

<build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml
      
    
  


你可能感兴趣的:(springboot使用Mybatis的步骤)