文档信息整理
This commit is contained in:
parent
39bdf37640
commit
cec89b67a6
@ -1,5 +1,5 @@
|
||||
[id]: 2018-11-09
|
||||
[type]: 软件使用
|
||||
[type]: 软件
|
||||
[tag]: linux,mongodb
|
||||
|
||||
  首先到官网下载安装包,官网地址如下:[点击跳转](https://www.mongodb.com/download-center/community),选中合适的版本,下面会出现下载链接,然后使用 wget url 下载到当前文件夹下。mongodb 4.04 ubuntu18.04 64 下载命令如下:
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 62ee59a0d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-12_1
|
||||
[type]:javaee
|
||||
[tag]:java,spring
|
||||
|
||||
  spring是为了解决企业级应用开发的复杂性而创建的,spring最根本的使命是:简化Java开发。为降低开发复杂性有以下四种关键策略。
|
||||
|
||||
- 基于POJO的轻量级和最小侵入性编程
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 6d17f940d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-12_2
|
||||
[type]:javaee
|
||||
[tag]:java,spring
|
||||
|
||||
## 一、自动装配
|
||||
|
||||
### 1、四种类型的自动装配
|
||||
|
@ -1,6 +1,10 @@
|
||||
[id]: 6f135410d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-12_3
|
||||
[type]:javaee
|
||||
[tag]:java,spring
|
||||
|
||||
## 一.面向切面编程
|
||||
|
||||
|
||||
  Spring的基础是IOC和AOP,前面两节对IOC和DI做了简单总结,这里再对AOP进行一个学习总结,Spring基础就算有一个初步了解了。
|
||||
|
||||
  在软件开发中,我们可能需要一些跟业务无关但是又必须做的东西,比如日志,事务等,这些分布于应用中多处的功能被称为横切关注点,通常横切关注点从概念上是与应用的业务逻辑相分离的。如何将这些横切关注点与业务逻辑在代码层面进行分离,是面向切面编程(**AOP**)所要解决的。
|
||||
|
@ -1,4 +1,6 @@
|
||||
[id]: 729b1b40d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-13
|
||||
[type]:javaee
|
||||
[tag]:java,spring,springboot
|
||||
  前面的博客有说到spring boot搭建见另一篇博文,其实那篇博文还没写,现在来填个坑。我们使用spring initializr来构建,idea和eclipse都支持这种方式,构建过程类似,这里以idea为例,详细记录构建过程。
|
||||
|
||||
###1.选择spring initializr
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 7a77bda0d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-20
|
||||
[type]:javaee
|
||||
[tag]:java,spring,springsecurity,scurity
|
||||
|
||||
**说明springboot版本2.0.3**
|
||||
|
||||
##一、 介绍
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 7c140880d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-21
|
||||
[type]:javaee
|
||||
[tag]:java,spring,springsecurity,scurity
|
||||
|
||||
  紧接着上一篇,上一篇中登录验证都由security帮助我们完成了,如果我们想要增加一个验证码登录或者其它的自定义校验就没办法了,因此这一篇讲解如何实现这个功能。
|
||||
|
||||
##一、 实现自定义登录校验类
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 7cfc1e90d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-22
|
||||
[type]:javaee
|
||||
[tag]:java,spring,springsecurity,scurity
|
||||
|
||||
  这篇讲解如何自定义鉴权过程,实现根据数据库查询出的url和method是否匹配当前请求的url和method来决定有没有权限。security鉴权过程如下:
|
||||
![鉴权流程](./picFolder/pic2.png)
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 7e229d30d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-25
|
||||
[type]:javaee
|
||||
[tag]:java,spring,websocket
|
||||
|
||||
<h3 id="#一、背景">一、背景</h3>
|
||||
|
||||
  我们都知道http协议只能浏览器单方面向服务器发起请求获得响应,服务器不能主动向浏览器推送消息。想要实现浏览器的主动推送有两种主流实现方式:
|
||||
|
@ -1,363 +1,366 @@
|
||||
[id]: 818f3b40d6c111e8aa858b6a40460626
|
||||
## 写在前面
|
||||
|
||||
刚毕业的第一份工作是java开发,项目中需要用到mybatis,特此记录学习过程,这只是一个简单demo,mybatis用法很多不可能全部写出来,有更复杂的需求建议查看mybatis的官方中文文档,[点击跳转](http://www.mybatis.org/mybatis-3/zh/index.html)。下面时项目环境/版本。
|
||||
|
||||
- 开发工具:IDEA
|
||||
- jdk版本:1.8
|
||||
- springboot版本:2.03
|
||||
|
||||
其他依赖版本见下面pom.xml:
|
||||
|
||||
```xml
|
||||
<?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>com.example</groupId>
|
||||
<artifactId>mybatis-test</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>mybatis-test</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.3.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>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!--mybatis依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<!--alibaba连接池依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.9</version>
|
||||
</dependency>
|
||||
<!--分页依赖-->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
```
|
||||
|
||||
## 1.创建项目
|
||||
|
||||
使用idea中的spring initializr生成maven项目,项目命令为mybatis-test,选择web,mysql,mybatis依赖,即可成功。(详细过程不赘述,如有需要学习springboot创建过程,可参考[这篇文章]()。
|
||||
|
||||
然后依照上面的pom文件,补齐缺少的依赖。接着创建包entity,service和mybatis映射文件夹mapper,创建。为了方便配置将application.properties改成application.yml。由于我们时REST接口,故不需要static和templates目录。修改完毕后的项目结构如下:
|
||||
|
||||
![项目结构](./picFolder/pic1.png)
|
||||
|
||||
修改启动类,增加`@MapperScan("com.example.mybatistest.dao") `,以自动扫描dao目录,避免每个dao都手动加`@Mapper`注解。代码如下:
|
||||
|
||||
```java
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.example.mybatistest.dao")
|
||||
public class MybatisTestApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MybatisTestApplication.class, args);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
修改application.yml,配置项目,代码如下:
|
||||
|
||||
```yml
|
||||
mybatis:
|
||||
#对应实体类路径
|
||||
type-aliases-package: com.example.mybatistest.entity
|
||||
#对应mapper映射文件路径
|
||||
mapper-locations: classpath:mapper/*.xml
|
||||
|
||||
#pagehelper物理分页配置
|
||||
pagehelper:
|
||||
helper-dialect: mysql
|
||||
reasonable: true
|
||||
support-methods-arguments: true
|
||||
params: count=countSql
|
||||
returnPageInfo: check
|
||||
|
||||
server:
|
||||
port: 8081
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
name: mysqlTest
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
#druid连接池相关配置
|
||||
druid:
|
||||
#监控拦截统计的filters
|
||||
filters: stat
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
|
||||
username: root
|
||||
password: 123456
|
||||
#配置初始化大小,最小,最大
|
||||
initial-size: 1
|
||||
min-idle: 1
|
||||
max-active: 20
|
||||
#获取连接等待超时时间
|
||||
max-wait: 6000
|
||||
#间隔多久检测一次需要关闭的空闲连接
|
||||
time-between-eviction-runs-millis: 60000
|
||||
#一个连接在池中的最小生存时间
|
||||
min-evictable-idle-time-millis: 300000
|
||||
#打开PSCache,并指定每个连接上PSCache的大小。oracle设置为true,mysql设置为false。分库分表设置较多推荐设置
|
||||
pool-prepared-statements: false
|
||||
max-pool-prepared-statement-per-connection-size: 20
|
||||
http:
|
||||
encoding:
|
||||
charset: utf-8
|
||||
enabled: true
|
||||
```
|
||||
|
||||
## 2.编写代码
|
||||
|
||||
首先创建数据表,sql语句如下:
|
||||
|
||||
```sql
|
||||
CREATE TABLE `user` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) NOT NULL,
|
||||
`age` tinyint(4) NOT NULL DEFAULT '0',
|
||||
`password` varchar(255) NOT NULL DEFAULT '123456',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
|
||||
```
|
||||
|
||||
然后在entity包中创建实体类User.java
|
||||
|
||||
```java
|
||||
public class User {
|
||||
private int id;
|
||||
private String name;
|
||||
private int age;
|
||||
private String password;
|
||||
|
||||
public User(int id, String name, int age, String password) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.password = password;
|
||||
}
|
||||
public User(){}
|
||||
//getter setter自行添加
|
||||
}
|
||||
```
|
||||
|
||||
在dao包下创建UserDao.java
|
||||
|
||||
```java
|
||||
public interface UserDao {
|
||||
//插入用户
|
||||
int insert(User user);
|
||||
//根据id查询
|
||||
User selectById(String id);
|
||||
//查询所有
|
||||
List<User> selectAll();
|
||||
}
|
||||
```
|
||||
|
||||
在mapper文件夹下创建UserMapper.xml,具体的xml编写方法查看文首的官方文档。
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.example.mybatistest.dao.UserDao">
|
||||
<sql id="BASE_TABLE">
|
||||
user
|
||||
</sql>
|
||||
<sql id="BASE_COLUMN">
|
||||
id,name,age,password
|
||||
</sql>
|
||||
|
||||
<insert id="insert" parameterType="com.example.mybatistest.entity.User" useGeneratedKeys="true" keyProperty="id">
|
||||
INSERT INTO <include refid="BASE_TABLE"/>
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
name,password,
|
||||
<if test="age!=null">
|
||||
age
|
||||
</if>
|
||||
</trim>
|
||||
<trim prefix=" VALUE(" suffix=")" suffixOverrides=",">
|
||||
#{name,jdbcType=VARCHAR},#{password},
|
||||
<if test="age!=null">
|
||||
#{age}
|
||||
</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<select id="selectById" resultType="com.example.mybatistest.entity.User">
|
||||
select
|
||||
<include refid="BASE_COLUMN"/>
|
||||
from
|
||||
<include refid="BASE_TABLE"/>
|
||||
where id=#{id}
|
||||
</select>
|
||||
|
||||
<select id="selectAll" resultType="com.example.mybatistest.entity.User">
|
||||
select
|
||||
<include refid="BASE_COLUMN"/>
|
||||
from
|
||||
<include refid="BASE_TABLE"/>
|
||||
</select>
|
||||
</mapper>
|
||||
```
|
||||
|
||||
至此使用mybatis的代码编写完了,之后要用时调用dao接口中的方法即可。
|
||||
|
||||
## 3.测试
|
||||
|
||||
我们通过编写service,controller然后使用postman进行测试。
|
||||
|
||||
首先编写UserService.java,代码如下:
|
||||
|
||||
```java
|
||||
@Component
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
|
||||
public User getByUserId(String id){
|
||||
return userDao.selectById(id);
|
||||
}
|
||||
//获取全部用户
|
||||
public List<User> getAll(){
|
||||
return userDao.selectAll();
|
||||
}
|
||||
//测试分页
|
||||
public PageInfo<User> getAll(int pageNum,int pageSize){
|
||||
PageHelper.startPage(pageNum,pageSize);
|
||||
List<User> users = userDao.selectAll();
|
||||
System.out.println(users.size());
|
||||
PageInfo<User> result = new PageInfo<>(users);
|
||||
return result;
|
||||
}
|
||||
|
||||
public int insert(User user){
|
||||
return userDao.insert(user);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
编写UserController.java
|
||||
|
||||
```java
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/user/{userId}")
|
||||
public User getUser(@PathVariable String userId){
|
||||
return userService.getByUserId(userId);
|
||||
}
|
||||
|
||||
@GetMapping("/user")
|
||||
public List<User> getAll(){
|
||||
return userService.getAll();
|
||||
}
|
||||
|
||||
@GetMapping("/user/page/{pageNum}")
|
||||
public Object getPage(@PathVariable int pageNum,
|
||||
@RequestParam(name = "pageSize",required = false,defaultValue = "10") int pageSize){
|
||||
return userService.getAll(pageNum,pageSize);
|
||||
}
|
||||
|
||||
@PostMapping("/user")
|
||||
public Object addOne(User user){
|
||||
userService.insert(user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
启动项目,通过postman进行请求测试,测试结果如下:
|
||||
|
||||
- 插入数据:
|
||||
|
||||
![插入](./picFolder/pic2.png)
|
||||
|
||||
- 查询数据
|
||||
|
||||
![查询](./picFolder/pic3.png)
|
||||
|
||||
|
||||
|
||||
- 分页查询
|
||||
|
||||
![分页查询](./picFolder/pic4.png)
|
||||
|
||||
|
||||
|
||||
## 4.注解编写sql
|
||||
|
||||
上面使用的是xml方式编写sql代码,其实mybatis也支持在注解中编写sql,这样可以避免编写复杂的xml查询文件,但同时也将sql语句耦合到了代码中,也不易实现复杂查询,因此多用于简单sql语句的编写。
|
||||
|
||||
要使用注解首先将applicaton.yml配置文件中的`mapper-locations: classpath:mapper/*.xml`注释掉。然后在UserDao.java中加入sql注解,代码如下:
|
||||
|
||||
```java
|
||||
public interface UserDao {
|
||||
//插入用户
|
||||
@Insert("insert into user(name,age,password) value(#{name},#{age},#{password})")
|
||||
@Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id")
|
||||
int insert(User user);
|
||||
//根据id查询
|
||||
@Select("select * from user where id=#{id}")
|
||||
User selectById(String id);
|
||||
//查询所有
|
||||
@Select("select * from user")
|
||||
List<User> selectAll();
|
||||
}
|
||||
```
|
||||
|
||||
然后重新启动项目测试,测试结果跟上面完全一样。
|
||||
|
||||
```
|
||||
如果对你有帮助记得点赞、收藏哦!
|
||||
[id]:2018-09-01
|
||||
[type]:javaee
|
||||
[tag]:java,spring,mysql,mybatis,xml
|
||||
|
||||
## 写在前面
|
||||
|
||||
刚毕业的第一份工作是java开发,项目中需要用到mybatis,特此记录学习过程,这只是一个简单demo,mybatis用法很多不可能全部写出来,有更复杂的需求建议查看mybatis的官方中文文档,[点击跳转](http://www.mybatis.org/mybatis-3/zh/index.html)。下面时项目环境/版本。
|
||||
|
||||
- 开发工具:IDEA
|
||||
- jdk版本:1.8
|
||||
- springboot版本:2.03
|
||||
|
||||
其他依赖版本见下面pom.xml:
|
||||
|
||||
```xml
|
||||
<?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>com.example</groupId>
|
||||
<artifactId>mybatis-test</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>mybatis-test</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.3.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>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!--mybatis依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<!--alibaba连接池依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.9</version>
|
||||
</dependency>
|
||||
<!--分页依赖-->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
```
|
||||
|
||||
## 1.创建项目
|
||||
|
||||
使用idea中的spring initializr生成maven项目,项目命令为mybatis-test,选择web,mysql,mybatis依赖,即可成功。(详细过程不赘述,如有需要学习springboot创建过程,可参考[这篇文章]()。
|
||||
|
||||
然后依照上面的pom文件,补齐缺少的依赖。接着创建包entity,service和mybatis映射文件夹mapper,创建。为了方便配置将application.properties改成application.yml。由于我们时REST接口,故不需要static和templates目录。修改完毕后的项目结构如下:
|
||||
|
||||
![项目结构](./picFolder/pic1.png)
|
||||
|
||||
修改启动类,增加`@MapperScan("com.example.mybatistest.dao") `,以自动扫描dao目录,避免每个dao都手动加`@Mapper`注解。代码如下:
|
||||
|
||||
```java
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.example.mybatistest.dao")
|
||||
public class MybatisTestApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MybatisTestApplication.class, args);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
修改application.yml,配置项目,代码如下:
|
||||
|
||||
```yml
|
||||
mybatis:
|
||||
#对应实体类路径
|
||||
type-aliases-package: com.example.mybatistest.entity
|
||||
#对应mapper映射文件路径
|
||||
mapper-locations: classpath:mapper/*.xml
|
||||
|
||||
#pagehelper物理分页配置
|
||||
pagehelper:
|
||||
helper-dialect: mysql
|
||||
reasonable: true
|
||||
support-methods-arguments: true
|
||||
params: count=countSql
|
||||
returnPageInfo: check
|
||||
|
||||
server:
|
||||
port: 8081
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
name: mysqlTest
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
#druid连接池相关配置
|
||||
druid:
|
||||
#监控拦截统计的filters
|
||||
filters: stat
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
|
||||
username: root
|
||||
password: 123456
|
||||
#配置初始化大小,最小,最大
|
||||
initial-size: 1
|
||||
min-idle: 1
|
||||
max-active: 20
|
||||
#获取连接等待超时时间
|
||||
max-wait: 6000
|
||||
#间隔多久检测一次需要关闭的空闲连接
|
||||
time-between-eviction-runs-millis: 60000
|
||||
#一个连接在池中的最小生存时间
|
||||
min-evictable-idle-time-millis: 300000
|
||||
#打开PSCache,并指定每个连接上PSCache的大小。oracle设置为true,mysql设置为false。分库分表设置较多推荐设置
|
||||
pool-prepared-statements: false
|
||||
max-pool-prepared-statement-per-connection-size: 20
|
||||
http:
|
||||
encoding:
|
||||
charset: utf-8
|
||||
enabled: true
|
||||
```
|
||||
|
||||
## 2.编写代码
|
||||
|
||||
首先创建数据表,sql语句如下:
|
||||
|
||||
```sql
|
||||
CREATE TABLE `user` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) NOT NULL,
|
||||
`age` tinyint(4) NOT NULL DEFAULT '0',
|
||||
`password` varchar(255) NOT NULL DEFAULT '123456',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
|
||||
```
|
||||
|
||||
然后在entity包中创建实体类User.java
|
||||
|
||||
```java
|
||||
public class User {
|
||||
private int id;
|
||||
private String name;
|
||||
private int age;
|
||||
private String password;
|
||||
|
||||
public User(int id, String name, int age, String password) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.password = password;
|
||||
}
|
||||
public User(){}
|
||||
//getter setter自行添加
|
||||
}
|
||||
```
|
||||
|
||||
在dao包下创建UserDao.java
|
||||
|
||||
```java
|
||||
public interface UserDao {
|
||||
//插入用户
|
||||
int insert(User user);
|
||||
//根据id查询
|
||||
User selectById(String id);
|
||||
//查询所有
|
||||
List<User> selectAll();
|
||||
}
|
||||
```
|
||||
|
||||
在mapper文件夹下创建UserMapper.xml,具体的xml编写方法查看文首的官方文档。
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.example.mybatistest.dao.UserDao">
|
||||
<sql id="BASE_TABLE">
|
||||
user
|
||||
</sql>
|
||||
<sql id="BASE_COLUMN">
|
||||
id,name,age,password
|
||||
</sql>
|
||||
|
||||
<insert id="insert" parameterType="com.example.mybatistest.entity.User" useGeneratedKeys="true" keyProperty="id">
|
||||
INSERT INTO <include refid="BASE_TABLE"/>
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
name,password,
|
||||
<if test="age!=null">
|
||||
age
|
||||
</if>
|
||||
</trim>
|
||||
<trim prefix=" VALUE(" suffix=")" suffixOverrides=",">
|
||||
#{name,jdbcType=VARCHAR},#{password},
|
||||
<if test="age!=null">
|
||||
#{age}
|
||||
</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<select id="selectById" resultType="com.example.mybatistest.entity.User">
|
||||
select
|
||||
<include refid="BASE_COLUMN"/>
|
||||
from
|
||||
<include refid="BASE_TABLE"/>
|
||||
where id=#{id}
|
||||
</select>
|
||||
|
||||
<select id="selectAll" resultType="com.example.mybatistest.entity.User">
|
||||
select
|
||||
<include refid="BASE_COLUMN"/>
|
||||
from
|
||||
<include refid="BASE_TABLE"/>
|
||||
</select>
|
||||
</mapper>
|
||||
```
|
||||
|
||||
至此使用mybatis的代码编写完了,之后要用时调用dao接口中的方法即可。
|
||||
|
||||
## 3.测试
|
||||
|
||||
我们通过编写service,controller然后使用postman进行测试。
|
||||
|
||||
首先编写UserService.java,代码如下:
|
||||
|
||||
```java
|
||||
@Component
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
|
||||
public User getByUserId(String id){
|
||||
return userDao.selectById(id);
|
||||
}
|
||||
//获取全部用户
|
||||
public List<User> getAll(){
|
||||
return userDao.selectAll();
|
||||
}
|
||||
//测试分页
|
||||
public PageInfo<User> getAll(int pageNum,int pageSize){
|
||||
PageHelper.startPage(pageNum,pageSize);
|
||||
List<User> users = userDao.selectAll();
|
||||
System.out.println(users.size());
|
||||
PageInfo<User> result = new PageInfo<>(users);
|
||||
return result;
|
||||
}
|
||||
|
||||
public int insert(User user){
|
||||
return userDao.insert(user);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
编写UserController.java
|
||||
|
||||
```java
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/user/{userId}")
|
||||
public User getUser(@PathVariable String userId){
|
||||
return userService.getByUserId(userId);
|
||||
}
|
||||
|
||||
@GetMapping("/user")
|
||||
public List<User> getAll(){
|
||||
return userService.getAll();
|
||||
}
|
||||
|
||||
@GetMapping("/user/page/{pageNum}")
|
||||
public Object getPage(@PathVariable int pageNum,
|
||||
@RequestParam(name = "pageSize",required = false,defaultValue = "10") int pageSize){
|
||||
return userService.getAll(pageNum,pageSize);
|
||||
}
|
||||
|
||||
@PostMapping("/user")
|
||||
public Object addOne(User user){
|
||||
userService.insert(user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
启动项目,通过postman进行请求测试,测试结果如下:
|
||||
|
||||
- 插入数据:
|
||||
|
||||
![插入](./picFolder/pic2.png)
|
||||
|
||||
- 查询数据
|
||||
|
||||
![查询](./picFolder/pic3.png)
|
||||
|
||||
|
||||
|
||||
- 分页查询
|
||||
|
||||
![分页查询](./picFolder/pic4.png)
|
||||
|
||||
|
||||
|
||||
## 4.注解编写sql
|
||||
|
||||
上面使用的是xml方式编写sql代码,其实mybatis也支持在注解中编写sql,这样可以避免编写复杂的xml查询文件,但同时也将sql语句耦合到了代码中,也不易实现复杂查询,因此多用于简单sql语句的编写。
|
||||
|
||||
要使用注解首先将applicaton.yml配置文件中的`mapper-locations: classpath:mapper/*.xml`注释掉。然后在UserDao.java中加入sql注解,代码如下:
|
||||
|
||||
```java
|
||||
public interface UserDao {
|
||||
//插入用户
|
||||
@Insert("insert into user(name,age,password) value(#{name},#{age},#{password})")
|
||||
@Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id")
|
||||
int insert(User user);
|
||||
//根据id查询
|
||||
@Select("select * from user where id=#{id}")
|
||||
User selectById(String id);
|
||||
//查询所有
|
||||
@Select("select * from user")
|
||||
List<User> selectAll();
|
||||
}
|
||||
```
|
||||
|
||||
然后重新启动项目测试,测试结果跟上面完全一样。
|
||||
|
||||
```
|
||||
如果对你有帮助记得点赞、收藏哦!
|
||||
```
|
@ -1,5 +1,8 @@
|
||||
[id]: 82f50cd0d6c111e8aa858b6a40460626
|
||||
# <div align="center">springboot与activeMQ入门(1)</div>
|
||||
[id]:2018-09-05
|
||||
[type]:javaee
|
||||
[tag]:java,spring,springboot,activemq
|
||||
|
||||
|
||||
**说明:acitveMQ版本为:5.9.1,springboot版本为2.0.3**<br/>
|
||||
## 一. 下载安装(windows)
|
||||
  官方下载地址:[点我跳转](http://activemq.apache.org/download-archives.html),选择windows安装包下载,然后解压,解压后运行bin目录下的**activemq.bat**启动服务,无报错即可启动成功。默认管理地址为:[localhost:8161/admin](localhost:8161/admin),默认管理员账号密码为**admin**/**admin**。
|
||||
|
@ -1,4 +1,8 @@
|
||||
[id]: 83e5d570d6c111e8aa858b6a40460626
|
||||
[id]:2018-09-06
|
||||
[type]:javaee
|
||||
[tag]:java,spring,activemq
|
||||
|
||||
|
||||
  单个MQ节点总是不可靠的,一旦该节点出现故障,MQ服务就不可用了,势必会产生较大的损失。这里记录activeMQ如何开启主从备份,一旦master(主节点故障),slave(从节点)立即提供服务,实现原理是运行多个MQ使用同一个持久化数据源,这里以jdbc数据源为例。同一时间只有一个节点(节点A)能够抢到数据库的表锁,其他节点进入阻塞状态,一旦A发生错误崩溃,其他节点就会重新获取表锁,获取到锁的节点成为master,其他节点为slave,如果节点A重新启动,也将成为slave。
|
||||
|
||||
主从备份解决了单节点故障的问题,但是同一时间提供服务的只有一个master,显然是不能面对数据量的增长,所以需要一种横向拓展的集群方式来解决面临的问题。
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 876b52b0d6c111e8aa858b6a40460626
|
||||
[id]:2018-09-10
|
||||
[type]:javaee
|
||||
[tag]:java,spring,springboot,mybatis,读写分离
|
||||
|
||||
  近日工作任务较轻,有空学习学习技术,遂来研究如果实现读写分离。这里用博客记录下过程,一方面可备日后查看,同时也能分享给大家(网上的资料真的大都是抄来抄去,,还不带格式的,看的真心难受)。
|
||||
|
||||
[完整代码](https://github.com/FleyX/demo-project/tree/master/dxfl)
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 88926d90d6c111e8aa858b6a40460626
|
||||
[id]:2018-09-20
|
||||
[type]:软件
|
||||
[tag]:git,crlf,lf
|
||||
|
||||
  项目组最近加了一个新功能到代码中,使用flyway记录数据库版本变更,,,该工具会记录每次数据库结构的修改并生成sql文件存在指定目录上(当然必须用它来变更数据库,外部的变更它是无法感知的),然后每次启动时flyway会检查使用的数据库和当前项目代码中的sql变更版本是否一致,一致正常启动,不一致中如果是数据库落后将会更新数据库(这样能够保证代码在任何地方运行数据库都是一致的),否则就报错了。数据库中有一张表记录版本信息,如下图:
|
||||
|
||||
![版本记录](./picFolder/版本记录.PNG),同时本地代码中也有一个文件夹保存每次操作的sql语句,如下图:
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 8940d600d6c111e8aa858b6a40460626
|
||||
[id]:2018-09-22
|
||||
[type]:java
|
||||
[tag]:java,reflect,excel,hssfworksheet
|
||||
|
||||
## 一、背景
|
||||
|
||||
  最近在java上做了一个EXCEL的导出功能,写了一个通用类,在这里分享分享,该类支持多sheet,且无需手动进行复杂的类型转换,只需提供三个参数即可:
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 8b08c4c0d6c111e8aa858b6a40460626
|
||||
[id]:2018-09-30
|
||||
[type]:java
|
||||
[tag]:java,正则,pattern
|
||||
|
||||
##一、背景
|
||||
|
||||
  今天领导让我写几个正则表达式来对密码做强度验证,听到写正则表达式内心是这样的感觉(哈哈,三分钟搞定,今天又可以打鱼了)。需求如下:密码组成只能是**数字**,**字母**,**英文可见半角符号**,然后需要如下4个表达式:
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 8c5ac030d6c111e8aa858b6a40460626
|
||||
[id]:2018-10-01
|
||||
[type]:项目
|
||||
[tag]:node,vue,element-ui,axios,koa,redis,mysql
|
||||
|
||||
  毕业才刚刚两个多月而已,现在想想大学生活是那么的遥不可及,感觉已经过了好久好久,社会了两个月才明白学校的好啊。。。额,扯远了,自从毕业开始就想找个时间写下毕设的记录总结,结果找了好久好久到今天才开始动笔。
|
||||
|
||||
  我的毕业设计题目是:教学辅助系统的设计与实现,,是不是很俗。。。至于为啥是这个题目呢,完全是被导师坑了。。。。。
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 8fdce210d6c111e8aa858b6a40460626
|
||||
[id]:2018-10-02
|
||||
[type]:数据库
|
||||
[tag]:acid,sql,分布式,事务
|
||||
|
||||
## 前言
|
||||
|
||||
  分布式事务是企业集成中的一个技术难点,也是每一个分布式系统架构中都会涉及到的一个东西,特别是在微服务架构中,几乎是无法避免的。
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 912d2fd0d6c111e8aa858b6a40460626
|
||||
[id]:2018-10-03
|
||||
[type]:数据库
|
||||
[tag]:acid,sql,分布式,事务
|
||||
|
||||
## 一、 数据库事务四大特性ACID
|
||||
|
||||
### 1、 原子性(Atomicity)
|
||||
|
@ -1,4 +1,7 @@
|
||||
[id]: 92887a10d6c111e8aa858b6a40460626
|
||||
[id]:2018-08-04
|
||||
[type]:软件
|
||||
[tag]:vmware,vps,nat,ubuntu
|
||||
|
||||
## 1、wmware设置
|
||||
|
||||
  这篇记录下nat网络模式下虚拟机与主机的相互ping通。首先使用wmware建立一个ubuntu虚拟机,网络模式选择nat模式。然后点击虚拟网络编辑:
|
||||
|
Loading…
x
Reference in New Issue
Block a user