public interface UserRepository extends JpaRepository {
UserEntity findByAccountAndPassword(String account, String password);
@Query(value = "select * from user u where u.account like ?1", nativeQuery = true)
List userlist(String account);
List findByAccountContaining(String account);
//多表查询可以不用实体返回
@Query(value = "select t.*,u.family,u.frirends,u.job,u.school from mydb.user t left join mydb.userinfo u on t.id=u.id", nativeQuery = true)
List
@Service("UserService")
public class UserService implements UserInterface {
@Autowired
private UserRepository userRepository;
//查
public UserEntity GetEntityByPassword(String account, String password) {
return userRepository.findByAccountAndPassword(account, password);
}
//增
//@Transactional(rollbackOn = Exception.class)//开启事务,报错回滚
public boolean Register(UserEntity userEntity) {
boolean res = false;
if (!userEntity.getAccount().isEmpty() && !userEntity.getPassword().isEmpty()) {
String pass = DigestUtils.md5DigestAsHex(userEntity.getPassword().getBytes());
userEntity.setPassword(pass);
userEntity.setId(BaseController.getUUID());
userRepository.save(userEntity);
res = true;
}
return res;
}
//查
public List userlist(String account){
System.out.println("这里在读数据库");
return userRepository.userlist(account);
}
//增
public UserEntity Adduser(UserEntity userEntity){
userRepository.save(userEntity);
return userEntity;
}
//分页查
public List pagelist(Integer page, Integer size){
Sort sort=new Sort(new Sort.Order(Sort.Direction.ASC,"account"));
//构造分页对象
Pageable pageable =new PageRequest(page,size,sort);
Page pageusers= userRepository.findAll(pageable);
List pagelist=pageusers.getContent();
long total=pageusers.getTotalElements();
return pagelist;
}
//多表查
public List
public interface UserInterface {
UserEntity GetEntityByPassword(String account, String password);
//用户注册
boolean Register(UserEntity userEntity);
List userlist(String account);
List pagelist(Integer page, Integer size);
UserEntity Adduser(UserEntity userEntity);
List
JPA多表查询返回结果
使用mybatis增改查
@Mapper
public interface UserMaper {
public List Sel();
public UserEntity Getbyid(String id);
public List Getbyword(String word);
public void insertdata(UserEntity userEntity);
public void updatedata(UserEntity userEntity);
public List selecttwo();
public List
xml代码
insert user (id,name,address,phone,account)values (#{id},#{name},#{address},#{phone},#{account})
update user
name=#{name},address=#{address},phone=#{phone},account=#{account}
where id=#{id}
控制器代码
@RestController
@Slf4j
public class TestController {
@Autowired
private UserInterface userInterface;
@Autowired
private UserMaper userMaper;
@RequestMapping("get/testcache")
public List findone(String account) {
if (account == null) {
account = "haha";
}
return userInterface.userlist(account);
}
@RequestMapping("get/adduser")
public UserEntity Adduser() {
UserEntity userEntity = new UserEntity();
userEntity.setId("1234");
userEntity.setAccount("haha");
userInterface.Adduser(userEntity);
return userEntity;
}
@RequestMapping("get/getcachelist")
public List findcachelist(String account) {
Jedis jedis = new Jedis("localhost", 6379);
jedis.connect();
Set value = jedis.keys("users");
jedis.disconnect();
return userInterface.userlist(account);
}
@RequestMapping("get/savestring")
@Cacheable("user_string")
public String savestring() {
String value = "dfsdfhesrwefsvdsdvivkdfifasndfhfi";
return value;
}
/*测试使用mybatis连接数据
* mybatis持久化框架
* */
@RequestMapping("get/mybatisdata")
public List get() {
return userMaper.Sel();
}
@RequestMapping("get/mybatisuser")
public UserEntity getbyid(String id) {
return userMaper.Getbyid(id);
}
@RequestMapping("get/getbyword")
public List getbyword(String word) {
return userMaper.Getbyword(word);
}
@RequestMapping("get/insert")
public String insertdata() {
UserEntity user=new UserEntity();
user.setId(UUID.randomUUID().toString());
user.setAccount("ceshibatis");
user.setAddress("奥尔良");
user.setName("新宿");
user.setPhone("13778790989");
userMaper.insertdata(user);
return "保存成功!";
}
@RequestMapping("get/update")
public String updatedata(String id) {
UserEntity user=new UserEntity();
user.setId(id);
user.setAccount("update");
user.setAddress("update奥尔良");
user.setName("update新宿");
user.setPhone("13778790989");
userMaper.updatedata(user);
return "更新成功!";
}
@RequestMapping("get/gettwo")
public List gettwo() {
return userMaper.selecttwo();
}
@RequestMapping("get/gettwobymap")
public List
原文地址:http://www.open-open.com/lib/view/open1346857871615.html
使用Java Mail API来发送邮件也很容易实现,但是最近公司一个同事封装的邮件API实在让我无法接受,于是便打算改用Spring Mail API来发送邮件,顺便记录下这篇文章。 【Spring Mail API】
Spring Mail API都在org.spri