SpringBoot构建电商基础秒杀项目练习(一)

慕课网SpringBoot构建电商基础秒杀项目

Mybatis接入SpringBoot项目

在pom.xml中编辑,先创建mysql-connector端的java连接

      <dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.41</version>
		</dependency>

采用阿里druid连接池管理

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.5</version>
		</dependency>

application.properties配置文件中,导入mybatis需要的一些配置,用来启动一 个带有mybatis数据库访问的SpringBoot项目工程

 mybatis.mapperLocation=classpath:mappong/*.xml

然后在resources下创建mapping文件夹
在这里插入图片描述
引入mybatis自动生成文件的插件

			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				
				<dependencies>
				<!-- 对mysql解析 -->
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.41</version>
					</dependency>
				</dependencies>
	
				<executions>
					<execution>
						<id>mybatis generator</id>
						<phase>package</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<!-- 考虑到项目中反复生成文件的情况 -->
				<configuration>
					<!-- 允许移动生成的文件 -->
					<verbose>true</verbose>
					<!-- 允许自动覆盖文件 -->
					<overwrite>true</overwrite>
					<!-- mybatis generator配置文件的路径 -->
					<configurationFile>
						src/main/resources/mybatis-generator.xml
					</configurationFile>
				</configuration>			

用户模块

使用 mybatis-generator 自动生成代码,只能根据主键查询,添加相应的方法

在UserPasswordDOMapper 添加:

UserPasswordDO selectByUserId(Integer userId);

在UserPasswordDoMapper.xml 添加

<select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user_password
    where user_id = #{userId,jdbcType=INTEGER}
  </select>

data object 是mybatis-generator 生成的与数据库相映射,视频里提出企业级开发中不能直接把dao 传输给前端,所以建立service层逻辑模型 model,同时UserModel 中封装了UserDOMapper,UserPasswordDOMapper,使用UserModel传递会返回一些不必要的信息(密码),所以在controller层定义了VO类传递。

UserModel

public class UserModel {

 private Integer id;
 private String name;
 private Byte gender;
 private Integer age;
 private String telphone;
 private String registerMode;
 private String thirdPartyId;
 private String encrptPassword;
 ...
 }

service

public interface UserService {
    UserModel getUserById(Integer id);
}

实现接口

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDOMapper userDOMapper;

    @Autowired
    private UserPasswordDOMapper userPasswordDOMapper;

    @Override
    public UserModel getUserById(Integer id){
        UserDO userDO = userDOMapper.selectByPrimaryKey(id);

        if(userDO == null){
            return null;
        }

        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());

        return convertFromDataObject(userDO, userPasswordDO);

    }

   private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
        if (userDO == null) {
            return null;
        }
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO, userModel);
        if (userPasswordDO != null) {
            userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
        }
        return userModel;
    }
}

UserVO

public class UserVO {

    private Integer id;
    private String name;
    private Byte gender;
    private Integer age;
    private String telphone;
    ...
    }

UserController

@Controller("user")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/get")
    @ResponseBody
    public UserVO getUser(@RequestParam(name="id") Integer id){
        UserModel userModel = userService.getUserById(id);
        return convertFromModel(userModel);
    }
//userModel--->userVO
    private UserVO convertFromModel(UserModel userModel){
        if(userModel == null){
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userModel, userVO);
        return userVO;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值