深入学习java笔记-9.SpringBoot2.1之Mybat
2019-05-08 本文已影响0人
笨鸡
pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
application.yml
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_boot_mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: *********
User.java
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {
private Integer id;
private String username;
@JsonInclude(JsonInclude.Include.NON_NULL)
private Integer age;
@JsonProperty("account")
private String phone;
@JsonIgnore
private String email;
}
UserDao.java
import com.ctgu.springstart.domain.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserDao {
/**
* 根据用户名称,查询用户信息
* @param username 用户名
*/
@Select("select * from user where username = #{username}")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "age", column = "age"),
@Result(property = "phone", column = "phone"),
@Result(property = "email", column = "email"),
})
User findByName(String username);
@Select("select * from user where id = #{id}")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "age", column = "age"),
@Result(property = "phone", column = "phone"),
@Result(property = "email", column = "email"),
})
User findById(Integer id);
@Select("select * from user")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "age", column = "age"),
@Result(property = "phone", column = "phone"),
@Result(property = "email", column = "email")
})
List<User> getAllUser();
@Update("update user set username=#{username} where id=#{id}")
void updateUser(User user);
@Delete("delete from user where id =#{id}")
void deleteUser(User user);
}
UserService.java
import com.ctgu.springstart.domain.User;
import java.util.List;
import java.util.Map;
public interface UserService {
User findUserByName(String username);
User findUserById(Integer id);
List<User> getAllUser();
Map<String, Object> updateUser(String name, Integer id);
void deleteUser(User user);
}
UserServiceImpl.java
import com.ctgu.springstart.dao.UserDao;
import com.ctgu.springstart.domain.User;
import com.ctgu.springstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public User findUserByName(String username) {
return userDao.findByName(username);
}
@Override
public User findUserById(Integer id) {
return userDao.findById(id);
}
@Override
public List<User> getAllUser() {
return userDao.getAllUser();
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public Map<String, Object> updateUser(String name, Integer id) {
Map<String, Object> map = new HashMap<>();
User user = findUserById(id);
if(null == user){
map.put("isUpdate", false);
map.put("result", null);
}else{
user.setUsername(name);
userDao.updateUser(user);
// int b = 1/0; //开启异常事务会回滚
map.put("isUpdate", true);
map.put("result", findUserById(id));
}
return map;
}
@Override
public void deleteUser(User user) {
userDao.deleteUser(user);
}
}