赞
踩
李老师讲的课基于实战,因此很多淘汰的或者繁琐的写法都留给我们课下实操。所以说,绝对是干货满满。
1. 核心组件
在Spring Boot的环境下前三个是不用我们自己手写的,全自动。
2. 使用MyBatis对用户表进行CRUD操作
第三步,在application中做出配置,这里是我的配置文件,可以参考
- server.port=8080
- server.servlet.context-path=/community
- spring.thymeleaf.cache=false
-
- # DataSourceProperties 配置mysql数据库和连接池
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.url=jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
- spring.datasource.username=root
- spring.datasource.password=password
- spring.datasource.type=com.zaxxer.hikari.HikariDataSource
- spring.datasource.hikari.maximum-pool-size=15
- spring.datasource.hikari.minimum-idle=5
- spring.datasource.hikari.idle-timeout=30000
-
- # MybatisProperties
- mybatis.mapper-locations=classpath:mapper/*.xml
- mybatis.type-aliases-package=com.nowcoder.community.entity
- mybatis.configuration.useGeneratedKeys=true
- mybatis.configuration.mapUnderscoreToCamelCase=true
- # logger
- logging.level.com.nowcoder.community=debug
- @Mapper
- public interface UserMapper {
- User selectById(int id);
- User selectByName(String username);
- int insertUser(User user);
- int updateStatus(int id,int status);
- int updatePassword(int id,String password);
- }
- <?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.nowcoder.community.dao.UserMapper">
- <sql id="selectFields">
- id,username,password,status
- </sql>
- <sql id="insertFields">
- id,username,password,status
- </sql>
- <insert id="insertUser" keyProperty="id" parameterType="User">
- insert into user(<include refid="insertFields"></include>)
- values(#{username},#{password},#{status})
- </insert>
- <update id="updateStatus">
- update user set status = #{status} where id = #{id}
- </update>
- <update id="updatePassword">
- update user set password = #{password} where id = #{id}
- </update>
- <select id="selectById" resultType="User">
- select <include refid="selectFields"></include>
- from user
- where id = #{id}
- </select>
- <select id="selectByName" resultType="User">
- select <include refid="selectFields"></include>
- from user
- where id = #{id}
- </select>
- </mapper>
- public class MapperTests {
- @Autowired
- private UserMapper userMapper;
- @Test
- public void testSelectUser(){
- User user = userMapper.selectById(1);
- System.out.println(user);
- }
- }
成功查询到数据,其余方法不再一一展示。建议没学过MyBatis的小伙伴先去学习一个黑马的MyBatis视频(14集)同样是干货满满。接下来就进入到了正式开发的环节
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。