Problems encountered in using mybatis

1. The base class mapper using

@RequestMapping("")
@MapperScan("awsa.jatham.cloud.platform.mapper")
@Component
public class H2Client {
    //注入mappper
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DataNodeMapper dataNodeMapper;
    public  static H2Client h2Client;

    @PostConstruct //导入bean
    public void init(){
        h2Client = this;
        h2Client.dataNodeMapper = this.dataNodeMapper;
    }

2.mybatis & mysql field as a key issue

//使用反引号解决 esc下面的键位
    <insert id="insert" parameterType="awsa.jatham.cloud.platform.entity.Constellation"  keyProperty="id">
        insert into constellation(date,name,`number`,`all`)
        values(#{date,jdbcType=INTEGER},
        #{name,jdbcType=VARCHAR},
         #{number,jdbcType=INTEGER},
         #{all,jdbcType=VARCHAR})

    </insert>

3.springboot integration process mybatis

  1. Add dependent

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.0</version>
    </dependency>
    
  2. Write a simple java class

  3. Write a method mapper interface design needs to be implemented

@Repository(value = "UserMapper")
public interface UserMapper {
    User getById(int id);
    public boolean insert(String name);
    public List<User> getUsers();
    public boolean updateUser(User user);
    public boolean deleteUser(int id);
    public boolean deleteAllUsers();
}
  1. Write mapper.xml implementation mapper interface
<?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="awsa.jatham.cloud.platform.mapper.UserMapper">

    <!--id对应接口中的方法,名字要一样,parameterType是方法的参数类型,
    resultType是查询返回的类型,需要注意的是,这里的SQL语句后面不能加分号,变量不能加引号-->
    <select id="getById" parameterType="int" resultType="awsa.jatham.cloud.platform.entity.User">
        select * from user where id = #{id}
    </select>

    <insert id="insert" parameterType="string">
        insert into user(name) values(#{name})
    </insert>

    <select id="getUsers"  resultType="awsa.jatham.cloud.platform.entity.User">
        select * from user order by age
    </select>

    <update id="updateUser" parameterType="awsa.jatham.cloud.platform.entity.User">
        update user set name=#{name} where id = #{id}
    </update>
    <delete id="deleteAllUsers">
        delete from user
    </delete>
    <delete id="deleteUser" parameterType="int">
        delete from user where id = #{id}
    </delete>

</mapper>

5. Preparation Service

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User getById(int id){
        return userMapper.getById(id);
    }
    public List<User> getByUsers()
    {
        return userMapper.getUsers();
    }

}

6.controller in writing interface calls

@CrossOrigin // 解决跨域问题
@RestController
@RequestMapping("")
public class IndexController {
    //H2Client h2Client = new H2Client();
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    H2Client h2Client = new H2Client();
    HashMap<String,Integer>params = new HashMap<String, Integer>();
    @Autowired //自动装配
    private UserService userService;
    @Autowired
    private DataNodeMCUService dataNodeMCUService;

    @GetMapping("/dataNodeMCU")
    public StringBuilder index()
    {
        logger.info(""+h2Client.dataFromAli());
        System.out.println(h2Client.dataFromAli());
        return h2Client.dataFromAli();

    }
    @PostMapping("/dataInsert")
    public void insertData(@RequestParam(value = "ifHuman",required = true)Integer ifHuman)
    {
        params.clear();
        params.put("ifHuman",ifHuman);
        dataNodeMCUService.insertBatch(params);

    }

    @GetMapping("/users")
    public List<User> user()
    {

        return userService.getByUsers();
    }

The last configuration .yml or properties

#spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# 定义匹配静态资源路径
spring.mvc.static-path-pattern=/**
# 定义静态资源位置
spring.resources.static-locations=classpath:/templates,classpath:/resources/,classpath:/static/,classpath:/public/




#datasource
spring.datasource.tomcat.driver-class-name=com.mysql.jdbc.Driver

#mybatis
spring.datasource.url=jdbc:mysql://101.xxx.xxx.199:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&useSSL=false
spring.datasource.username=
spring.datasource.password=
spring.datasource.tomcat.default-auto-commit=true

mybatis.typeAliasesPackage= awsa.jatham.cloud.platform.mapper
mybatis.mapper-locations=classpath:mapper/*.xml
Released three original articles · won praise 0 · Views 53

Guess you like

Origin blog.csdn.net/qq_42784606/article/details/103043470