Spring boot学习(一)------Idea+Springboot+maven+SpringMVC+Mybatis(SSM)构建简单项目

 

小子初学者,刚从大学走出踏入社会,在工作岗位中发现工作所得远远不足以满足自己的知识所求。所以利用工作闲暇之时,学习了一下简单的Springboot相关的知识。在此简单记录一下所学所得以及在学习过程中的各种坑。希望能够对大家有所帮助!如果有不足之处希望大家指出!

各种原理介绍在此不再赘述,大家都可以百度得到或者看官方文档。我就简单将就一下开发过程,另外此次开发使用的是Idea。

1.新建Springboot项目

File ----> New ----->Spring Initializr

                       点击next

可以选择maven或者gradle作为依赖管理,这里选择maven。

添加项目依赖,暂时只需添加Web,Mybatis,Mysql三个依赖,点击Next.r然后一路next就可以完成项目的创建!

2.配置mybatis配置文件

在resources目录下创建application.properties文件,其中Spring.datasources代表数据库信息,

mybatis.typeAliasesPackage=com.ssm.entity 代表对应的entity所在路径

mybatis.mapper-locations=classpath*:/mapper/UserMapper.xml代表Mapper所在的位置。

在resources文件夹之下新建一个文件夹mapper,用于存储mapper.xml文件。UserMapper.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.ssm.dao.UserDao">
    <resultMap id="BaseResultMap" type="com.ssm.entity.User">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="age" jdbcType="INTEGER" property="age" />
    </resultMap>
    <sql id="Base_Column_List">
        id, name, age
    </sql>
    <select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ssm
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deletePrimaryKey" parameterType="java.lang.Integer">
        delete from ssm
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" parameterType="com.ssm.entity.User">
        insert into ssm (id, name, age
        )
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
        )
    </insert>
    <insert id="insertSelective" parameterType="com.ssm.entity.User">
        insert into ssm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="age != null">
                age,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="age != null">
                #{age,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.ssm.entity.User">
        update ssm
        <set>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="age != null">
                age = #{age,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ssm.entity.User">
        update ssm
        set name = #{name,jdbcType=VARCHAR},
        age = #{age,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
    </update>
</mapper>

完成mybatis位置之后开始coding!

3.构建Spring MVC 三层架构

最终的项目目录结构如上:

其中UserController.java代码如下:

package com.ssm.controller;

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

/**
 * Created by mac on 2018/8/29.
 */

@RestController
public class UserController {



    @Autowired
    private UserService userService;

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public void  addUser(String name,int age){
        User user = new User(name,age);
        userService.save(user);
    }

    @RequestMapping(value = "/deleteUser",method = RequestMethod.GET)
    public boolean deleteUserById(int id){
        return userService.deleteById(id);
    }


    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public boolean deleteUserById(int id,String name, int age){
        User user = new User(id,name,age);
        return userService.updateById(user);
    }


    @RequestMapping(value = "/queryUser",method = RequestMethod.GET)
    public User getUserById(int id){
        System.out.println(id);
        return userService.getUser(id);
    }
}

其中UserDao.java代码如下: 

package com.ssm.dao;

import com.ssm.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserDao {
    int deletePrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectById(int id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

其中User.java代码如下:  

package com.ssm.entity;



import java.io.Serializable;

/**
 * Created by mac on 2018/8/29.
 */
public class User implements Serializable {

    private int id;
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

其中UserService.java代码如下:  

package com.ssm.service;

import com.ssm.entity.User;
import org.springframework.stereotype.Repository;

/**
 * Created by mac on 2018/8/29.
 */
@Repository
public interface UserService {
    void save(User user);
    User getUser(int id);
    boolean updateById(User user);
    boolean deleteById(int id);
}

其中UserServiceImpl.java代码如下:   

package com.ssm.service.Impl;

import com.ssm.dao.UserDao;
import com.ssm.entity.User;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by mac on 2018/8/29.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;
    @Override
    public void save(User user) {
        userDao.insert(user);

    }

    @Override
    public User getUser(int id) {
        return userDao.selectById(id);
    }

    @Override
    public boolean updateById(User user) {
        return userDao.updateByPrimaryKey(user) > 0;
    }

    @Override
    public boolean deleteById(int id) {
        return userDao.deletePrimaryKey(id) > 0;
    }
}

至此,项目完成coding!运行项目是,SsmApplication.java作为入口文件进行运行。

package com.ssm;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@MapperScan("com.ssm.dao")

@EnableCaching
public class SsmApplication {

	public static void main(String[] args) {

		SpringApplication.run(SsmApplication.class, args);
	}
}

另外,项目采用的是mySQL数据库,并且创建了一个简单的user表,表结构如下:

 

当项目启动成功之后,在浏览器端输入

 4.总结

在此项目的开发过程中需要了解到几个点:

1.@RestController  @Service @Repository 三个注解的意思

2.UserDao中的函数方法名就是UserMapper.xml中的id,与Spring中整合的Mybatis来说更加的简洁明了

如果你想要查看完整代码,点击我的github进行查看!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值