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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ming_key/article/details/81388297

关键配置:启动类必须加上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

扫描二维码关注公众号,回复: 5872901 查看本文章
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>

项目结构图

猜你喜欢

转载自blog.csdn.net/Ming_key/article/details/81388297