java mapper control_java spring boot中怎么编写mapper?怎么编写service和controller?
上次已經(jīng)為大家介紹過(guò)java spring boot中怎么加入依賴的主要內(nèi)容了。今天再為大家介紹一些與之相關(guān)的內(nèi)容,也就是java spring
boot中怎么 編寫mapper以及怎么編寫service和controller?一起來(lái)了解一下吧。
首先說(shuō)一下,java spring boot中怎么編寫mapper?
代碼如下所示:import?java.util.List;
import?org.apache.ibatis.annotations.Mapper;
import?org.apache.ibatis.annotations.Select;
import?cn.cnn.info.pojo.User;
//extends?com.github.abel533.mapper.Mapper:需要繼承通用Mapper
@Mapper
public?interface?UserMapper?extends?com.github.abel533.mapper.Mapper?
{
@Select("select?*?from?user?where?name?like?'%${value}%'")
public?List??queryUserByName(String?name);
//?使用UserMapper.xml配置文件
public?List??queryAll();
}
然后說(shuō)一下怎么編寫service和controller?
Service編寫,代碼如下所示:import?java.util.List;
import?org.springframework.beans.factory.annotation.Autowired;
import?org.springframework.stereotype.Service;
import?com.github.pagehelper.PageHelper;
import?cn.cnn.info.dao.UserDao;
import?cn.cnn.info.dao.UserMapper;
import?cn.cnn.info.pojo.User;
import?cn.cnn.info.service.UserService;
@Service
public?class?UserServiceImpl?implements?UserService
{
@Autowired
private?UserDao?userDao;
@Autowired
private?UserMapper?userMapper;
@Override
public?List??findAll()
{
List??list?=?this.userDao.findAll();
return?list;
}
@Override
public?List??queryUserByName(String?name)
{
List??list?=?this.userMapper.queryUserByName(name);
return?list;
}
//?調(diào)用使用UserMapper.xml的Mapper
@Override
public?List??queryAll()
{
List??list?=?this.userMapper.queryAll();
return?list;
}
//?使用通用Mapper和分頁(yè)助手
@Override
public?List??queryUserByPage(Integer?page,?Integer?rows)
{
//?設(shè)置分頁(yè)
PageHelper.startPage(page,?rows);
//?使用通用Mapper的方法進(jìn)行查詢所有數(shù)據(jù)
List??list?=?this.userMapper.select(null);
return?list;
}
}
Controller編寫,代碼展示如下:import?java.util.List;
import?org.springframework.beans.factory.annotation.Autowired;
import?org.springframework.web.bind.annotation.PathVariable;
import?org.springframework.web.bind.annotation.RequestMapping;
import?org.springframework.web.bind.annotation.RestController;
import?cn.cnn.info.pojo.User;
import?cn.cnn.info.service.UserService;
@RestController
@RequestMapping("user")
public?class?UserControlelr
{
@Autowired
private?UserService?userService;
@RequestMapping("list")
public?List??queryUserAll()
{
List??list?=?this.userService.findAll();
return?list;
}
@RequestMapping("list/{name}")
public?List??queryUserAll(@PathVariable?String?name)
{
List??list?=?this.userService.queryUserByName(name);
return?list;
}
@RequestMapping("list/query")
public?List??queryUserAll2()
{
List??list?=?this.userService.queryAll();
return?list;
}
@RequestMapping("list/{page}/{rows}")
public?List??queryUserAll(@PathVariable?Integer?page,?@PathVariable?Integer?rows)
{
List??list?=?this.userService.queryUserByPage(page,?rows);
return?list;
}
}
以上就是關(guān)于java spring boot中怎么
編寫mapper以及怎么編寫service和controller的主要內(nèi)容了。具體的內(nèi)容還是比較簡(jiǎn)單易懂的。如果你對(duì)java知識(shí)感興趣,想要了解更多java實(shí)例和常見(jiàn)問(wèn)題,敬請(qǐng)關(guān)注奇Q工具網(wǎng)。
推薦閱讀:
總結(jié)
以上是生活随笔為你收集整理的java mapper control_java spring boot中怎么编写mapper?怎么编写service和controller?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java连接数据库hei_如何黑MySQ
- 下一篇: java webservice 常用_复