前言:搞android的 有时候要用的后台接口,还是一件比较麻烦的事情。最近看了下SpringBoot试了下,以后再也不担心没有接口用了。^_^ !! 那我们就先实现一个登陆注册和查询吧,最后再实现接口数据统一封装格式。,
数据库:mysql5.6
一,表结构
1,用户表
2,歌单表
3,歌曲表
二,功能演示:
1,登陆功能,
①,接口
②,dao接口数据库操作
③,实现接口,
@Service
public class UserServiceimpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void addUser(User user) {
userDao.addUser(user);
}
@Override
public int queryUserByNumber(String number) {
return userDao.queryUserByNumber(number);
}
@Override
public boolean updateUserById() {
return false;
}
@Override
public boolean deleteUserById() {
return false;
}
@Override
public List<User> queryUserList() {
return userDao.queryUserList();
}
@Override
public User userLogin(String number) {
User user = userDao.userLogin(number);
return user;
}
@Override
public int userReg(User user){
return userDao.userReg(user);
}
}
④,控制层
@RestController
public class UserController {
@Autowired
private UserService userService;
ResultUtil resultUtil = new ResultUtil();
@RequestMapping(value = "/query", method = RequestMethod.GET)
public Result queryUserList() {
return resultUtil.success(userService.queryUserList());
}
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public void addUser(String name, String age) {
User user = new User();
userService.addUser(user);
}
@PostMapping(value = "/userLogin/{number}/{pwd}")
public Result userLogin(@PathVariable("number") String number, @PathVariable("pwd") String pwd) {
if (number.length() == 0 || pwd.length() == 0) {
User u = userService.userLogin(number);
if (u != null) {
if (u.getNumber() == null) {
return resultUtil.success("账号不存在");
} else if (!u.getPassword().equals(pwd)) {
return resultUtil.success("密码错误");
}
}
return resultUtil.success(u);
} else {
return resultUtil.success("请输入账号和密码!");
}
}
@PostMapping(value = "/userReg")
public Result userReg(@RequestBody User user) {
//todo先查询一下。判断有无已经注册
int result=userService.queryUserByNumber(user.getNumber());
if (result==1){//chaxuncheng
return resultUtil.success("注册失败,用户名已存在!");
}else{
int us= userService.userReg(user);
System.out.println(user.toString());
if (us==1){
return resultUtil.success("注册成功");
}
}
return resultUtil.success("注册失败");
}
}
⑤Mapper层
<?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.cwj.music.dao.UserDao">
<resultMap id="BaseResultMap" type="com.cwj.music.entity.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="number" jdbcType="VARCHAR" property="number" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="image" jdbcType="VARCHAR" property="image" />
</resultMap>
<select id="queryUserList" resultMap="BaseResultMap">
select * from user
</select>
<select id="userLogin" resultType="com.cwj.music.entity.User">
select * from user where number = #{number}
</select>
<!-- 注册 -->
<insert id="userReg" parameterType="com.cwj.music.entity.User" >
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user(name,number,password,image) values (#{name},#{number},#{password},#{image})
</insert>
<!--注册之前根据用户名查下-->
<select id="queryUserByNumber" resultType="java.lang.Integer">
select count(*) from user where number=#{number}
</select>
</mapper>
三:结果
这个有点不好写。我就直接把项目放在github上面把。有兴趣的可以看看。