SpringBoot(六)实操玩一下

SpringBoot(六)实操玩一下

在这里插入图片描述

新增德鲁伊连接池

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.3</version>
</dependency>
#配置端口号
server.port=8090

#MySQL数据库连接配置
spring.datasource.name=druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123

#整合mybatis
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

可以使用IDEA插件 Free Mybatis plugin 生成实体类和对应的Mapper文件

@Data
public class User implements Serializable {
    private Integer id;

    private String username;

    private String password;

    private String birthday;

    private static final long serialVersionUID = 1L;
}
public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

    List<User> queryAll();
}
<?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.szx.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.szx.pojo.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="birthday" jdbcType="VARCHAR" property="birthday" />
  </resultMap>
  <sql id="Base_Column_List">
    id, username, `password`, birthday
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <select id="queryAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.szx.pojo.User" useGeneratedKeys="true">
    insert into user (username, `password`, birthday
      )
    values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{birthday,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.szx.pojo.User" useGeneratedKeys="true">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="username != null">
        username,
      </if>
      <if test="password != null">
        `password`,
      </if>
      <if test="birthday != null">
        birthday,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null">
        #{birthday,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.szx.pojo.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        `password` = #{password,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null">
        birthday = #{birthday,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.szx.pojo.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      `password` = #{password,jdbcType=VARCHAR},
      birthday = #{birthday,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>
public interface UserService {

    /**
     * 查询所有
     * @return
     */
    List<User> queryAll();

    /**
     * 通过id查询
     * @param id
     * @return
     */
    User findById(Integer id);

    /**
     * 插入
     * @param user
     * @return
     */
    Integer insert(User user);

    /**
     * 根据id删除
     * @param id
     * @return
     */
    Integer deleteById(Integer id);

    /**
     * 修改
     * @param user
     * @return
     */
    Integer update(User user);

}
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> queryAll() {
        return userMapper.queryAll();
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insert(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public Integer deleteById(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * restful风格进行访问
     *  查询类请求为:get
     *  新增类请求为:post
     *  更新类请求为:put
     *  删除类请求为:delete
     */

    @Autowired
    private UserService userService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/queryAll")
    public List<User> queryAll(){
        return userService.queryAll();
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public User findById(@PathVariable Integer id){
        return userService.findById(id);
    }

    /**
     * 插入
     * @param user
     * @return
     */
    @PostMapping("/insert")
    public String insert(User user){
        Integer insert = userService.insert(user);
        if (insert > 0){
            return "插入成功!";
        } else {
            return "插入失败!";
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public String deleteById(@PathVariable Integer id){
        Integer deleteById = userService.deleteById(id);
        if (deleteById > 0){
            return "删除成功!";
        } else {
            return "删除失败!";
        }
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @PutMapping("/update")
    public String update(User user){
        Integer update = userService.update(user);
        if (update > 0){
            return "修改成功!";
        } else {
            return "修改失败!";
        }
    }

}
@SpringBootApplication
@MapperScan("com.szx.mapper")     //扫描mybatis的包
public class Springbootdemo3Application {

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值