社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
官网下载比较慢,我这有2.4版本的groovy安装包。https://pan.baidu.com/s/147PdfFQbeUY53jJcYm4wlw
安装之后如同jdk配置一样,将bin目录加入到系统环境变量中.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ink.toppest</groupId>
<artifactId>groovy_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>groovy_test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
MybatisConfig.java
package ink.toppest.groovy_test.config;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* 描述:
*
* @author HASEE
* @create 2018-10-23 21:03
*/
@Configuration
public class MybatisConfig {
@Bean
@Primary
MybatisProperties mybatisProperties(){
MybatisProperties mybatisProperties=new MybatisProperties();
org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
config.setMapUnderscoreToCamelCase(true);
mybatisProperties.setConfiguration(config);
return mybatisProperties;
}
}
Book.groovy
package ink.toppest.groovy_test.domain
class Book {
long id
String name
String author
Date date
}
BookMapper.java
package ink.toppest.groovy_test.mapper;
/**
* 描述:
*
* @author HASEE
* @create 2018-10-23 21:07
*/
import ink.toppest.groovy_test.domain.Book;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* Created by jack on 2017/4/15.
*/
@Mapper
public interface BookMapper {
@Select("select * from book where id = #{id}")
List<Book> findByState(@Param("id") long id);
@Select("select * from book")
List<Book> findAll();
@Insert({
"insert into book",
"set name = #{b.name},",
"author = #{b.author},",
"date = #{b.date},",
})
@Options(useGeneratedKeys = true, keyProperty = "id")
//使用@Options注解的userGeneratedKeys 和keyProperty属性让数据库产生auto_increment(自增长)列的值,然后将生成的值设置到输入参数对象的属性中。
Book insert(@Param("b") Book book) throws RuntimeException;
}
BookController.groovy
package ink.toppest.groovy_test.controller
import ink.toppest.groovy_test.domain.Book
import ink.toppest.groovy_test.mapper.BookMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
@Controller
class BookController {
@Autowired
BookMapper bookMapper
@GetMapping("/book")
String getAll(Map map){
List<Book> list=bookMapper.findAll()
map.put("books",list)
"list"
}
}
list.html
<html xmlns:th="http://www.thymeleaf.org">
<html lang="zh">
<body>
<br>
<div th:each="book:${books}" >
书名:<p th:text=" ${book.name}"></p>
出版日期:<p th:text="${#dates.format(book.date, 'yyyy-MM-dd')}"></p>
作者:<p th:text="${book.author}"></p>
</div>
</body>
</html>
数据库sql
CREATE DATABASE `book` /*!40100 DEFAULT CHARACTER SET utf8 */;
CREATE TABLE `book` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`date` date DEFAULT NULL,
`author` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!