欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot入门(第三课,连接数据库)

程序员文章站 2022-04-05 08:04:55
...

关键配置:启动类必须加上mapper扫描位置,@MapperScan("com.jbz.jbzspringboot.mapper")

package com.jbz.jbzspringboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
@MapperScan("com.jbz.jbzspringboot.mapper")
public class JbzSpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(JbzSpringbootApplication.class, args);
		System.out.println("启动成功");
	}
}

 

application.properties配置加上:

#指出xml位置 
mybatis.mapperLocations=classpath:mapper/*.xml

 

maven引入依赖包

<!-- 引入mybatis starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
            <!--<scope>runtime</scope>-->
        </dependency>

        <!-- MySQL的JDBC驱动包	-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- 引入第三方数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>

controller:

package com.jbz.jbzspringboot.controller;

import com.jbz.jbzspringboot.entity.User;
import com.jbz.jbzspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mingjian xu
 * @date 2018-08-03
 * @describe 解释用途
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("add")
    public int addUser(){
        User user = new User(22, "xmj", "pm", "1qaz2wsx","2018-07-31 23:00:00");
        int id = userService.add(user);
        return id;
    }

    @RequestMapping("select")
    public Object selectAll(){

        return userService.selectAll();
    }


}

 

service

 

package com.jbz.jbzspringboot.service;

import com.jbz.jbzspringboot.entity.User;

import java.util.List;

/**
 * @author mingjian xu
 * @date 2018-08-03
 * @describe 解释用途
 */
public interface UserService {

    int add(User user);

    List<User> selectAll();
}

serviceimpl

package com.jbz.jbzspringboot.service.impl;

import com.jbz.jbzspringboot.entity.User;
import com.jbz.jbzspringboot.mapper.user.UserMapper;
import com.jbz.jbzspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mingjian xu
 * @date 2018-08-03
 * @describe 解释用途
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public int add(User user) {
        mapper.insert(user);
        return user.getId();
    }

    @Override
    public List<User> selectAll() {
        return mapper.selectAll();
    }
}

mapper

package com.jbz.jbzspringboot.mapper.user;

import com.jbz.jbzspringboot.entity.User;

import java.util.List;

/**
 * @author mingjian xu
 * @date 2018-08-03
 * @describe 解释用途
 */
public interface UserMapper {

    /**
     * 保存用户
     * @param user
     * @return
     */
    int insert(User user);

    /**
     * 查询用户
     * @return
     */
    List<User> selectAll();
}

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.jbz.jbzspringboot.mapper.user.UserMapper">

  <select id="selectAll" resultType="com.jbz.jbzspringboot.entity.User">
    select * from user;
  </select>
  
  <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.jbz.jbzspringboot.entity.User">
    insert into user (`age`,`name`,`desc`,`password`,`createtime`) values (#{age},#{name},#{desc},#{password},#{createTime})
  </insert>



</mapper>

项目结构图

Spring Boot入门(第三课,连接数据库)