Java学习之SpringBoot + Mybatis+Sql Server

今天进行了尝试,学了SSM后首次使用Springboot整合MyBatis,因为学院要求数据库课设需要使用SqlServer,然后今天晚上就进行尝试。


下面是我的代码笔记。

首先是文件目录:

Java学习之SpringBoot + Mybatis+Sql Server_第1张图片

首先我们先进行SqlServer的配制
  • 设置一个可以登录的用户名
  • 创建一个数据库
  • 创建一张表
接着是Springboot的快速搭建
  • 具体搭建过程就不演示了,下面是我的pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.1.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.oldbaigroupId>
    <artifactId>moneyartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>moneyname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.3version>
        dependency>

        <dependency>
            <groupId>com.microsoft.sqlservergroupId>
            <artifactId>mssql-jdbcartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>com.microsoft.sqlservergroupId>
            <artifactId>sqljdbc4artifactId>
            <version>4.0version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

下面是目录下几个包的代码:
  • 按照我习惯的顺序,先是对数据库的操作(model层、dao层和mapper),然后到服务层对接controller层(server、server/impl、)最后是controller)
model 层
  • User.java 定义一个user对象对应数据库中的表
package com.oldbai.money.model;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

dao 层
  • UserMapper.java
package com.oldbai.money.dao;

import com.oldbai.money.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;

import java.util.List;
//这是对接 MyBatis 连接数据库的的XML 文件的层次
@Repository
public interface UserMapper {
    public List<User> findAll();
}

mapper 层
  • UserMapper.xml 对数据进行具体操作



<mapper namespace="com.oldbai.money.dao.UserMapper">

    <resultMap id="userMap" type="com.oldbai.money.model.User">
        <id property="id" column="id">id>
        <result property="username" column="username">result>
        <result property="password" column="password">result>
    resultMap>
    <select id="findAll" resultMap="userMap">
        select * from student
    select>

mapper>
server 层
  • userServer.java
package com.oldbai.money.server;

import com.oldbai.money.model.User;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
//服务层接口 对接的是 controller
public interface UserServer {
    public List<User> findAll();
}

server/impl 层
  • UserServerImpl.java
package com.oldbai.money.server.impl;

import com.oldbai.money.dao.UserMapper;
import com.oldbai.money.model.User;
import com.oldbai.money.server.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//实现服务
@Service
public class UserServerImpl implements UserServer {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }
}

controller 层
  • UserController.java
package com.oldbai.money.controller;

import com.oldbai.money.model.User;
import com.oldbai.money.server.UserServer;
import com.oldbai.money.server.impl.UserServerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping(value = "/User")
@RestController
public class UserController {
//    @Resource
    @Autowired
    private UserServer userServer;

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    @ResponseBody
    public List<User> findAllUser(){
        return userServer.findAll();
    }

    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public String findAllUser1(){
        return "helloworld";
    }

}

application.properties 配制文件,配制数据库信息和mybatis信息
  • application.properties
server.port=8088

spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=asset_manage
spring.datasource.username=sa
spring.datasource.password=sa123456789
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.oldbai.**.model

启动类添加扫包
package com.oldbai.money;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,同时,使用@MapperScan注解多个包
@SpringBootApplication
@MapperScan(value = {"com.oldbai.*.dao","com.oldbai.*.mapper"})
public class MoneyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MoneyApplication.class, args);
    }

}


基本上配制如上,就能简单的访问数据库了。
Java学习之SpringBoot + Mybatis+Sql Server_第2张图片

你可能感兴趣的:(Java学习,mybatis,数据库,java)