Spring Boot实践应用开发(6)

35 篇文章 0 订阅

【使用thymeleaf生成html


1,新建mybatissql文自动生成配置文件,generateConfig_sample.xml

<?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>
	<classPathEntry
		location="E:/pleiades/workspace/sample/src/main/resources/lib/ojdbc6.jar" />
	<context id="context1">
		<jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl" userId="sample"
			password="sample" />

		<javaModelGenerator targetPackage="org.com.sample.dataset"
			targetProject="sample" />
		<sqlMapGenerator targetPackage="org.com.sample.mapper"
			targetProject="sample" />
		<javaClientGenerator targetPackage="org.com.sample.mapper"
			targetProject="sample" type="XMLMAPPER" />

		<table tableName="Users" />

	</context>
</generatorConfiguration>




2,自动生成dataset,mapper等相关文件。

sample
|_src/main/java
  |_org.com.sample
    |_dataset
      |_Users.java
      |_UserExample.java
    |_mapper
      |_UsersMapper.java
      |_UsersMapper.xml

 

把UsersMapper.xml移动到resources文件夹下

|_src/main/resources
  |_org.com.sample
    |_mapper
      |_UsersMapper.java

 

3,在ApplicationConfig.java里,定义Mapper的bean

 

<pre name="code" class="java">Resource[] resources = new Resource[] { getMapperXMLPathResource(UsersMapper.class)
		};
 

@Bean
	public UsersMapper getUsersMapper() {
		SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(getSqlSessionFactory());
		return sessionTemplate.getMapper(UsersMapper.class);
	}


 

4,新规UsersService接口,内容和UsersMapper一样。

 

package org.com.sample.service;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.com.sample.dataset.Users;
import org.com.sample.dataset.UsersExample;

public interface UsersService {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int countByExample(UsersExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int deleteByExample(UsersExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int deleteByPrimaryKey(String username);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int insert(Users record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int insertSelective(Users record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    List<Users> selectByExample(UsersExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    Users selectByPrimaryKey(String username);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int updateByExampleSelective(@Param("record") Users record, @Param("example") UsersExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int updateByExample(@Param("record") Users record, @Param("example") UsersExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int updateByPrimaryKeySelective(Users record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table USERS
     *
     * @mbggenerated Sat Oct 10 16:15:21 JST 2015
     */
    int updateByPrimaryKey(Users record);
}


 

5,新规UsersService的实现类,UsersServiceImpl.java

 

package org.com.sample.service.impl;

import java.util.List;

import org.com.sample.dataset.Users;
import org.com.sample.dataset.UsersExample;
import org.com.sample.mapper.UsersMapper;
import org.com.sample.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl implements UsersService {

	@Autowired
	UsersMapper usersMapper;

	@Override
	public int countByExample(UsersExample example) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteByExample(UsersExample example) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteByPrimaryKey(String username) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(Users record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertSelective(Users record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Users> selectByExample(UsersExample example) {

		return usersMapper.selectByExample(example);
	}

	@Override
	public Users selectByPrimaryKey(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByExampleSelective(Users record, UsersExample example) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateByExample(Users record, UsersExample example) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateByPrimaryKeySelective(Users record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateByPrimaryKey(Users record) {
		// TODO Auto-generated method stub
		return 0;
	}

}

6,新规UsersFacade接口


package org.com.sample.facade;

import java.util.List;

import org.com.sample.dataset.Users;
import org.com.sample.dataset.UsersExample;

public interface UsersFacade {

	List<Users> selectByExample(UsersExample example);
}

7,新规 UsersFacade 接口的实现类, UsersFacadeImpl.java

 

package org.com.sample.facade.impl;

import java.util.List;

import org.com.sample.dataset.Users;
import org.com.sample.dataset.UsersExample;
import org.com.sample.facade.UsersFacade;
import org.com.sample.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UsersFacadeImpl implements UsersFacade {

	@Autowired
	UsersService usersService;

	@Override
	public List<Users> selectByExample(UsersExample example) {

		return usersService.selectByExample(example);
	}

}


8,新规UsersController.java

package org.com.sample.controller;

import java.util.List;

import org.com.sample.dataset.Users;
import org.com.sample.dataset.UsersExample;
import org.com.sample.facade.UsersFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/sample/users")
public class UsersController {

	private static final Logger logger = LoggerFactory.getLogger(UsersController.class);

	@Autowired
	UsersFacade userFacade;

	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String selectAllUser(Model model) {

		List<Users> userList = userFacade.selectByExample(new UsersExample());
		model.addAttribute("page", userList);
		return "sample/users/users";
	}

}

 

9,新规users.html

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Users</title>
<meta
	content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'
	name='viewport' />

<meta th:replace="head-cssjs" />

</head>
<body class="skin-blue">
	<div class="wrapper">
		<!-- Content Wrapper. Contains page content -->
		<div class="content-wrapper">
			<!-- Content Header (Page header) -->
			<section class="content-header">
				<h1>Users</h1>
			</section>

			<!-- Main content -->
			<section class="content">
				<div class="row">
					<div class="col-md-12">
						<form id="menuForm" method="post" class="form-horizontal">
							<div id="maincontent" class="box box-primary"
								style="overflow-y: auto; overflow-x: hidden;">
								<div class="box-body">
									<div class="form-group">
										<div class="col-md-12">
											<div class="box box-success"
												th:attrappend="style=${page == null ? 'display: none;' : ''}">
												<div class="col-md-12" style="width: 99%;">
													<table id="searchResult"
														class="table table-condensed table-striped table-hover">
														<thead>
															<tr>
																<th class="col-md-1">No.</th>
																<th class="col-md-1">username</th>
																<th class="col-md-1">password</th>
																<th class="col-md-1">role</th>
															</tr>
														</thead>
														<tbody>
															<tr th:each="content, iterStat : ${page}">
																<td class="col-md-1" th:id="${iterStat.index}"
																	th:name="${iterStat.index}"
																	th:value="${iterStat.index}"
																	th:text="${iterStat.index} + 1"></td>
																<td class="col-md-1"
																	th:id="'page['+${iterStat.index}+'].username'"
																	th:name="'page['+${iterStat.index}+'].username'"
																	th:value="${content.username}"
																	th:text="${content.username}"></td>
																<td class="col-md-1"
																	th:id="'page['+${iterStat.index}+'].password'"
																	th:name="'page['+${iterStat.index}+'].password'"
																	th:value="${content.password}"
																	th:text="${content.password}"></td>
																<td class="col-md-1"
																	th:id="'page['+${iterStat.index}+'].role'"
																	th:name="'page['+${iterStat.index}+'].role'"
																	th:value="${content.role}" th:text="${content.role}"></td>
															</tr>
														</tbody>
													</table>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</section>
			<!-- /.content -->
		</div>
		<!-- /.content-wrapper -->
	</div>
	<!-- ./wrapper -->

	<div th:replace="bottom-js"></div>
</body>
</html>

 10.启动项目,输入URL,http://localhost:8888/sample/users/show



代码下载地址

http://pan.baidu.com/s/1o6spOJS,fourth.zip


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot是企业级开发的整体整合解决方案,特别用于快速构建微服务应用,旨在用最简单的方式让开发人员适应各种开发场景; 本视频着重介绍SpringBoot的使用和内部原理;内容包含微服务概念、配置文件、日志框架的使用、web开发、Thymeleaf模板引擎、Docker容器技术、MyBatisSpring Data JPA、自定义starter等; 00、尚硅谷_SpringBoot_源码、课件 01、尚硅谷_SpringBoot_入门-课程简介 02、尚硅谷_SpringBoot_入门-Spring Boot简介 03、尚硅谷_SpringBoot_入门-微服务简介 04、尚硅谷_SpringBoot_入门-环境准备 05、尚硅谷_SpringBoot_入门-springboot-helloworld 06、尚硅谷_SpringBoot_入门-HelloWorld细节-场景启动器(starter) 07、尚硅谷_SpringBoot_入门-HelloWorld细节-自动配置 08、尚硅谷_SpringBoot_入门-使用向导快速创建Spring Boot应用 09、尚硅谷_SpringBoot_配置-yaml简介 10、尚硅谷_SpringBoot_配置-yaml语法 11、尚硅谷_SpringBoot_配置-yaml配置文件值获取 12、尚硅谷_SpringBoot_配置-properties配置文件编码问题 13、尚硅谷_SpringBoot_配置-@ConfigurationProperties与@Value区别 14、尚硅谷_SpringBoot_配置-@PropertySource、@ImportResource、@Bean 15、尚硅谷_SpringBoot_配置-配置文件占位符 16、尚硅谷_SpringBoot_配置-Profile多环境支持 17、尚硅谷_SpringBoot_配置-配置文件的加载位置 18、尚硅谷_SpringBoot_配置-外部配置加载顺序 19、尚硅谷_SpringBoot_配置-自动配置原理 20、尚硅谷_SpringBoot_配置-@Conditional&自动配置报告 21、尚硅谷_SpringBoot_日志-日志框架分类和选择 22、尚硅谷_SpringBoot_日志-slf4j使用原理 23、尚硅谷_SpringBoot_日志-其他日志框架统一转换为slf4j 24、尚硅谷_SpringBoot_日志-SpringBoot日志关系 25、尚硅谷_SpringBoot_日志-SpringBoot默认配置 26、尚硅谷_SpringBoot_日志-指定日志文件和日志Profile功能 27、尚硅谷_SpringBoot_日志-切换日志框架 28、尚硅谷_SpringBoot_web开发-简介 29、尚硅谷_SpringBoot_web开发-webjars&静态资源映射规则 30、尚硅谷_SpringBoot_web开发-引入thymeleaf 31、尚硅谷_SpringBoot_web开发-thymeleaf语法 32、尚硅谷_SpringBoot_web开发-SpringMVC自动配置原理 33、尚硅谷_SpringBoot_web开发-扩展与全面接管SpringMVC 34、尚硅谷_SpringBoot_web开发-【实验】-引入资源 35、尚硅谷_SpringBoot_web开发-【实验】-国际化 36、尚硅谷_SpringBoot_web开发-【实验】-登陆&拦截器 37、尚硅谷_SpringBoot_web开发-【实验】-Restful实验要求 38、尚硅谷_SpringBoot_web开发-【实验】-员工列表-公共页抽取 39、尚硅谷_SpringBoot_web开发-【实验】-员工列表-链接高亮&列表完成 40、尚硅谷_SpringBoot_web开发-【实验】-员工添加-来到添加页面 41、尚硅谷_SpringBoot_web开发-【实验】-员工添加-添加完成 42、尚硅谷_SpringBoot_web开发-【实验】-员工修改-重用页面&修改完成 43、尚硅谷_SpringBoot_web开发-【实验】-员工删除-删除完成 44、尚硅谷_SpringBoot_web开发-错误处理原理&定制错误页面 45、尚硅谷_SpringBoot_web开发-定制错误数据 46、尚硅谷_SpringBoot_web开发-嵌入式Servlet容器配置修改 47、尚硅谷_SpringBoot_web开发-注册servlet三大组件 48、尚硅谷_SpringBoot_web开发-切换其他嵌入式Servlet容器 49、尚硅谷_SpringBoot_web开发-嵌入式Servlet容器自动配置原理 50、尚硅谷_SpringBoot_web开发-嵌入式Servlet容器启动原理 51、尚硅谷_SpringBoot_web开发-使用外部Servlet容器&JSP;支持 52、尚硅谷_SpringBoot_web开发-外部Servlet容器启动SpringBoot应用原理 53、尚硅谷_SpringBoot_Docker-简介 54、尚硅谷_SpringBoot_Docker-核心概念 55、尚硅谷_SpringBoot_Docker-linux环境准备 56、尚硅谷_SpringBoot_Docker-docker安装&启动&停止 57、尚硅谷_SpringBoot_Docker-docker镜像操作常用命令 58、尚硅谷_SpringBoot_Docker-docker容器操作常用命令 59、尚硅谷_SpringBoot_Docker-docker安装MySQL 60、尚硅谷_SpringBoot_数据访问-简介 61、尚硅谷_SpringBoot_数据访问-JDBC&自动配置原理 62、尚硅谷_SpringBoot_数据访问-整合Druid&配置数据源监控 63、尚硅谷_SpringBoot_数据访问-整合MyBatis(一)-基础环境搭建 64、尚硅谷_SpringBoot_数据访问-整合MyBatis(二)-注解版MyBatis 65、尚硅谷_SpringBoot_数据访问-整合MyBatis(二)-配置版MyBatis 66、尚硅谷_SpringBoot_数据访问-SpringData JPA简介 67、尚硅谷_SpringBoot_数据访问-整合JPA 68、尚硅谷_SpringBoot_原理-第一步:创建SpringApplication 69、尚硅谷_SpringBoot_原理-第二步:启动应用 70、尚硅谷_SpringBoot_原理-事件监听机制相关测试 71、尚硅谷_SpringBoot_原理-自定义starter

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值