发表于: 2021-01-15 22:59:35
1 1190
今天完成的事情:
mybatis generator代码生成
修改SQL
写banner-Controller
明天计划的事情:
继续写剩下的Controller
遇到的问题:
暂无
收获:
mybatis generator代码生成
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 用于指定全局配置文件,下面可以通过占位符的形式读取<properties>指定文件中的值。-->
<properties resource="db.properties"/>
<!-- context 是逆向工程的主要配置信息 -->
<!-- id:起个名字 -->
<!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 抑制警告 -->
<property name="suppressTypeWarnings" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
<!-- 是否生成注释代时间戳-->
<property name="suppressDate" value="true" />
</commentGenerator>
<!--jdbc的数据库连接-->
<!--这里是以变量的形式定义,具体的值在 db.properties 文件中-->
<!--jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC-->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!-- targetPackage:生成的实体类所在的包 -->
<!-- targetProject:生成的实体类所在的硬盘位置 -->
<!-- DataObject 类存放位置 -->
<javaModelGenerator targetPackage="com.kbk.model"
targetProject=".\src\main\java">
<!-- 是否允许子包 -->
<property name="enableSubPackages" value="false" />
<!-- 是否对modal添加构造函数 -->
<property name="constructorBased" value="true" />
<!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
<property name="trimStrings" value="true" />
<!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
<property name="immutable" value="false" />
</javaModelGenerator>
<!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
<sqlMapGenerator targetPackage="mapping"
targetProject=".\src\main\resources">
<!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.kbk.dao" targetProject=".\src\main\java">
<!-- 针对 oracle 数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--tableName:指定了表名
domainObjectName:指定了实体类的名称
-->
<table tableName="banner" domainObjectName="Banner" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="collection" domainObjectName="Collection" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="manager" domainObjectName="Manager" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="message" domainObjectName="Message" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="module" domainObjectName="Module" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="track" domainObjectName="Track" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="visitor" domainObjectName="Visitor" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="works" domainObjectName="Works" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="workshop" domainObjectName="Workshop" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
======
写了一个Controller
@Controller
@RequestMapping("/banner")
public class BannerController {
@Autowired
BannerService bannerService;
private static final Logger log= LogManager.getLogger(BannerController.class);
//添加banner
@RequestMapping(value ="addBanner",method = RequestMethod.POST)
public ModelAndView addBanner(Banner banner, ModelAndView modelAndView){
log.info(banner);
if (bannerService.insertSelective(banner)){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","添加成功");
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("json");
return modelAndView;
}
//删除banner
@RequestMapping(value = "deleteBanner",method = RequestMethod.POST)
public ModelAndView deleteBanner(@PathVariable Long id, ModelAndView modelAndView){
if(bannerService.deleteByPrimaryKey(id)) {
modelAndView.addObject("code", 200);
modelAndView.addObject("msg", "删除成功");
}else {
modelAndView.addObject("code", 404);
modelAndView.addObject("msg", "操作失败");
}
modelAndView.setViewName("json");
return modelAndView;
}
//更改banner
@RequestMapping(value = "updateBanner",method = RequestMethod.PUT)
public ModelAndView updateBanner(Banner banner,ModelAndView modelAndView){
if(bannerService.updateByPrimaryKeySelective(banner)){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","更新成功");
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("json");
return modelAndView;
}
/**
* 查询单条banner
* @param id
* @param modelAndView
* @return
*/
@RequestMapping(value = "/selectBanner/{id}",method = RequestMethod.GET)
public ModelAndView getBanner(@PathVariable Long id, ModelAndView modelAndView){
Banner banner=bannerService.selectByPrimaryKey(id);
if (banner!=null){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","查询成功");
modelAndView.addObject("banner",banner);
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("bannerJson1");
return modelAndView;
}
@RequestMapping(value = "/list",method = RequestMethod.GET)
public ModelAndView getBannerList(ModelAndView modelAndView){
List<Banner> bannerList=bannerService.selectAllBanner();
log.info(bannerList);
if (bannerList!=null){
modelAndView.addObject("code",200);
modelAndView.addObject("msg","查询成功");
modelAndView.addObject("bannerList",bannerList);
}else {
modelAndView.addObject("code",404);
modelAndView.addObject("msg","操作失败");
}
modelAndView.setViewName("bannerJson2");
return modelAndView;
}
}
测试
评论