本人已经开发JAVA有一段时间了,所以说的内容会需要一定的JAVA基础知识。
直接上代码吧:
1.创建MAVEN项目
创建一个格式为war的maven项目
下面列出所需要的包依赖:
<properties>
<java.version>1.8java.version>
<druid.version>1.0.27druid.version>
<fastjson.version>1.2.17fastjson.version>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.4.3.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<defaultGoal>compiledefaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>${java.version}source>
<target>${java.version}target>
<encoding>UTF8encoding>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
2.创建配置文件
在项目的src/main/resources下创建一个名为application.yml的文件
1).创建spring boot直接读取文件(application.yml)
文件内容:
spring: profiles: active: dev #dev表示开发环境配置,对应配置文件---application-dev.yml #pro表示线上环境配置,对应配置文件---application-pro.yml
2).创建开发环境配置文件
文件内容:
logging: #日志配置部分 file: /home/logs/bandaotixi/bandaotixi.log level: root: info spring: datasource: url: jdbc:mysql://localhost:3306/bandaotixi username: test password: test type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp mybatis: #*Mapper.java可以随便放,只要和mapper.xml对应上就行 mapper-locations: classpath*:com/bandaotixi/front/core/mapper/*.xml type-aliases-package: com.bandaoti.demo.backend.entity #这个插件是mybatis的分页查询 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
3.创建实体映射文件
可以用mybatis-generator.jar工具来生成mybatis对应的实体映射文件
以下是我修改过的工具包(1积分)
mybatis-generator-core-1.3.6.jar
此包可以生成自定义主键类型,譬如要生成uuid主键对应mysql数据的配置文件为:
<table tableName="user" domainObjectName="User"><generatedKey column="id" sqlStatement="SELECT replace(uuid(),'-','') FROM dual"/>table>
下面附上所有配置详情(文件名:generatorConfig.xml):
<generatorConfiguration>
<classPathEntry location="C:\Users\zengxiaowei\.m2\repository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar" />
<context id="fontinfo">
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
<jdbcConnection connectionURL="jdbc:mysql://locahost:3306/bandaotixi?useUnicode=true&characterEncoding=utf-8&useSSL=true" driverClass="com.mysql.jdbc.Driver" password="test" userId="test" />
<javaModelGenerator targetPackage="com.bandaotixi.front.core.entity" targetProject="bandaotixi-front-core/src/main/java" />
<sqlMapGenerator targetPackage="com.bandaotixi.front.core.mapper" targetProject="bandaotixi-front-core/src/main/java" />
<javaClientGenerator targetPackage="com.bandaotixi.front.core.dao" targetProject="bandaotixi-front-core/src/main/java" type="XMLMAPPER" />
<table tableName="user" domainObjectName="User"><generatedKey column="id" sqlStatement="SELECT replace(uuid(),'-','') FROM dual"/>table>
context>
generatorConfiguration>
UserMapper.java
User.java
UserExample.java
UserMapper.xml
3.创建Service
拿userService举栗子
1).创建UserService.java
package com.bandaotixi.front.core.service; import com.bandaotixi.front.core.entity.User; import com.github.pagehelper.PageInfo; public interface UserService { PageInfo
findByPage(Integer start, Integer size, User param); } 2).创建实现类UserServiceImpl.java
package com.bandaotixi.front.core.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.bandaotixi.front.core.dao.UserMapper; import com.bandaotixi.front.core.entity.User; import com.bandaotixi.front.core.entity.UserExample; import com.bandaotixi.front.core.service.UserService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public PageInfo
findByPage(Integer start,Integer size,User param){ PageHelper.startPage(start, size); UserExample example=new UserExample(); // example.createCriteria().andLoginNameLike("%"+param.getLoginName()+"%"); return new PageInfo (userMapper.selectByExample(example)); } }
4.创建UserController.java
package com.bandaotixi.front.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bandaotixi.front.core.entity.User;
import com.bandaotixi.front.core.service.UserService;
import com.github.pagehelper.PageInfo;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired private UserService userService;
@RequestMapping("findByPage")
public @ResponseBody PageInfo findByPage(){
User user=new User();
user.setLoginName("test");
return userService.findByPage(1, 2, user);
}
}
5.创建启动类Application.java
1).在tomcat中启动
package com.bandaotixi.front; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 程序入口 * * @author XiRuiQiang */ @SpringBootApplication @MapperScan(basePackages = "com.bandaotixi.front.core.dao") public class Application { //这里用tomcat启动时读取两遍,请大神帮忙,留言解决 }
2).直接用java命令运行
添加main方法直接启动
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
需要在配置文件中添加tomcat配置:
server: port: 8443
6.最后附上创建表sql
CREATE TABLE `user` (
`id` varchar(32) NOT NULL,
`avatar_url` varchar(255) DEFAULT NULL,
`burg` varchar(10) DEFAULT NULL,
`card_id` varchar(20) DEFAULT NULL,
`city` varchar(10) DEFAULT NULL,
`country` varchar(10) DEFAULT NULL,
`create_by` varchar(32) DEFAULT NULL,
`create_date` datetime DEFAULT NULL,
`gender` int(1) DEFAULT NULL,
`login_name` varchar(32) DEFAULT NULL,
`name` varchar(32) DEFAULT NULL,
`open_id` varchar(72) DEFAULT NULL,
`password` varchar(64) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`province` varchar(10) DEFAULT NULL,
`type` int(2) DEFAULT NULL,
`update_by` varchar(32) DEFAULT NULL,
`update_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
最后以你选择的方式启动项目访问
http://localhost:8080/user/findByPage.json