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

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

项目结构图

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值