📚git使用
🍐 git安装
若右击鼠标出现下图的选项 ,则证明安装成功
🍐 git环境配置
Git配置
查看配置 git config -l
或 git config --list
设置用户名和邮箱 (必要)
git config --global user.name "marvintian"
git config --global user.email "601494165@qq.com"
🍐 git基本理论
🍐 初始化本地库
git init
🍐本地仓库和远程仓库建立连接
git remote add origin 远程仓库地址
📚Eclipse
🍐 Eclipse使用
https://www.cnblogs.com/zz-newbie/p/13305487.html eclipse创建maven项目
https://www.cnblogs.com/zoro-zero/p/11607461.html eclipse创建springboot项目
智能提示设置
https://www.cnblogs.com/dongwenfei/p/8136601.html
Window→Preferences→Java→Editor→Content Assist
调整包结构
自动导包
🍎 HelloWord
🍍 方式一:使用maven手动创建
参考文档 https://www.cnblogs.com/LUA123/p/8110285.html
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<groupId>com.marvin</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
@SpringBootApplication
public class SpringBootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootTestApplication.class,args);
}
}
@RestController
public class HellowController {
@GetMapping("hello")
public String hello() {
return "hello world";
}
}
在浏览器访问http://localhost:8080/hello
🍍 方式二:使用插件创建
安装插件
搜索下面的插件
插件安装完成,重启
在newproject中看到下图,说明安装成功
点击Spring Starter Project
🍎 Git使用
🍍提交代码
?代表未提交
🍍拉取项目
🍍冲突解决
当修改了同一个class后,我们将自己修改的commit到自己本地仓库,在推到远程时先拉取远程最新的代码
若远程和本地有冲突,那么拉取是不成功的,并且代码有如下显示
项目图标也会变化,有标识的代表是有冲突的类
此时我们选择它,来为我们展示冲突并进行解决
会弹出如下界面,供我们选择到底要保留什么
把左边修改成我们最后要保留的样子并保存
保存完后要将这个弹框关掉,不然下次冲突后他依旧会在,并且误导你
若想要保留右边的东西,就把它粘贴过来,左边的东西不想要就删除掉
保存后,class上的>>>>>>标志就会消失,再将我们最后保留的样子进行commit
至此我们的冲突就解决完成了
🍎 Maven
很多IDE会自带maven,建议大家自行安装maven使用,不要使用自带的
🍍配置远程地址
在maven的安装目录下,我们找到 conf 下的 settings.xml,下图就是配置远程地址的位置
🍍配置本地仓库
默认使用的是 C盘 .m2下的
🍍Eclipse使用
mvn的命令
🍐Eclipse常见设置
🍎设置workspace编码格式为UTF-8
window->preferences->general->workspace
🍎 formatter设置
在代码进行格式化时,会将代码注释也格式化
🍎Code Templates设置
设置Code Templates的目的主要是为了统一各种注释的格式以及代码的模板
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="false" context="gettercomment_context" deleted="false" description="Comment for getter method" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name="gettercomment">
/**
* ${bare_field_name}.
*
* @return the ${bare_field_name}
* @since JDK 1.6
*/</template><template autoinsert="false" context="settercomment_context" deleted="false" description="Comment for setter method" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.settercomment" name="settercomment">
/**
* ${param}.
*
* @param ${param} the ${bare_field_name} to set
* @since JDK 1.6
*/</template><template autoinsert="false" context="constructorcomment_context" deleted="false" description="Comment for created constructors" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name="constructorcomment">
/**
* Creates a new instance of ${enclosing_type}.
*
* ${tags}
*/
</template><template autoinsert="false" context="filecomment_context" deleted="false" description="Comment for created Java files" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.filecomment" name="filecomment">
/**
* Project Name:${project_name}
* File Name:${file_name}
* Package Name:${package_name}
* Date:${date}${time}
* Copyright (c) ${year}, chenzhou1025@126.com All Rights Reserved.
*
*/</template><template autoinsert="false" context="typecomment_context" deleted="false" description="Comment for created types" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.typecomment" name="typecomment">
/**
* ClassName: ${type_name} <br/>
* Function: ${todo} ADD FUNCTION. <br/>
* Reason: ${todo} ADD REASON(可选). <br/>
* date: ${date} ${time} <br/>
*
* @author ${user}
* @version ${enclosing_type}${tags}
* @since JDK 1.6
*/</template><template autoinsert="false" context="fieldcomment_context" deleted="false" description="Comment for fields" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name="fieldcomment">
/**
* ${field}:${todo}(用一句话描述这个变量表示什么).
* @since JDK 1.6
*/</template><template autoinsert="false" context="methodcomment_context" deleted="false" description="Comment for non-overriding methods" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name="methodcomment">
/**
* ${enclosing_method}:(这里用一句话描述这个方法的作用). <br/>
* ${todo}(这里描述这个方法适用条件 – 可选).<br/>
* ${todo}(这里描述这个方法的执行流程 – 可选).<br/>
* ${todo}(这里描述这个方法的使用方法 – 可选).<br/>
* ${todo}(这里描述这个方法的注意事项 – 可选).<br/>
*
* @author ${user}
* ${tags}
* @since JDK 1.6
*/</template><template autoinsert="false" context="overridecomment_context" deleted="false" description="Comment for overriding methods" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name="overridecomment">
/**
* ${todo} 简单描述该方法的实现功能(可选).
* ${see_to_overridden}
*/</template><template autoinsert="true" context="delegatecomment_context" deleted="false" description="Comment for delegate methods" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name="delegatecomment">
/**
* ${tags}
* ${see_to_target}
*/</template><template autoinsert="false" context="newtype_context" deleted="false" description="Newly created files" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.newtype" name="newtype">
/**
* Project Name:${project_name}
* File Name:${file_name}
* Package Name:${package_name}
* Date:${date}${time}
* Copyright (c) ${year}, chenzhou1025@126.com All Rights Reserved.
*
*/
${filecomment}
${package_declaration}
/**
* ClassName:${type_name} <br/>
* Function: ${todo} ADD FUNCTION. <br/>
* Reason: ${todo} ADD REASON. <br/>
* Date: ${date} ${time} <br/>
* @author ${user}
* @version
* @since JDK 1.6
* @see
*/
${typecomment}
${type_declaration}
</template><template autoinsert="true" context="classbody_context" deleted="false" description="Code in new class type bodies" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.classbody" name="classbody">
</template><template autoinsert="true" context="interfacebody_context" deleted="false" description="Code in new interface type bodies" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.interfacebody" name="interfacebody">
</template><template autoinsert="true" context="enumbody_context" deleted="false" description="Code in new enum type bodies" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.enumbody" name="enumbody">
</template><template autoinsert="true" context="annotationbody_context" deleted="false" description="Code in new annotation type bodies" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.annotationbody" name="annotationbody">
</template><template autoinsert="true" context="catchblock_context" deleted="false" description="Code in new catch blocks" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.catchblock" name="catchblock">
// ${todo} Auto-generated catch block
${exception_var}.printStackTrace();
</template><template autoinsert="false" context="methodbody_context" deleted="false" description="Code in created method stubs" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.methodbody" name="methodbody">
// ${todo} Auto-generated method stub
${body_statement}</template><template autoinsert="true" context="constructorbody_context" deleted="false" description="Code in created constructor stubs" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.constructorbody" name="constructorbody">
${body_statement}
// ${todo} Auto-generated constructor stub
</template><template autoinsert="true" context="getterbody_context" deleted="false" description="Code in created getters" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.getterbody" name="getterbody">return ${field};</template><template autoinsert="true" context="setterbody_context" deleted="false" description="Code in created setters" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.setterbody" name="setterbody">${field} = ${param};</template></templates>
🍐 快捷键
代码助手:Ctrl+Space(简体中文操作系统是Alt+/)
快速修正:Ctrl+1
单词补全:Alt+/
打开外部Java文档:Shift+F2
显示搜索对话框:Ctrl+H
快速Outline:Ctrl+O
打开资源:Ctrl+Shift+R
打开类型:Ctrl+Shift+T
显示重构菜单:Alt+Shift+T
上一个/下一个光标的位置:Alt+Left/Right
上一个/下一个成员(成员对象或成员函数):Ctrl+Shift+Up/Down
选中闭合元素:Alt+Shift+Up/Down/Left/Right
删除行:Ctrl+D
在当前行上插入一行:Ctrl+Shift+Enter
在当前行下插入一行: Shift+Enter
上下移动选中的行:Alt+Up/Down
📚 SpringData
🍧 入门案例
🥝创建项目
使用SpringBoot插件,或创建Maven项目均可
🥝依赖
基于SpringBoot创建
<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>
<!--Spring Data-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
🥝 创建POJO
@Entity
@Table(name = "product")
public class Product {
@Id
private Long id;
@Column(name = "productName")
private String productName;
@Column(name = "dir_id")
private Long dirId;
@Column(name = "salePrice")
private BigDecimal salePrice;
@Column(name = "supplier")
private String supplier;
@Column(name = "brand")
private String brand;
@Column(name = "cutoff")
private Double cutoff;
@Column(name = "costPrice")
private BigDecimal costPrice;
//get set 构造器
}
🥝创建持久层
@Repository
public interface ProductJPA extends JpaRepository<Product, Long> {
}
🥝创建控制层
@RestController
@RequestMapping("product")
public class ProductController {
@Autowired
private ProductJPA productJPA;
@GetMapping("list")
public List<Product> getList() {
return productJPA.findAll();
}
}
🥝配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///jdbcdemo
username: root
password: admin
jpa:
show-sql: true
hibernate:
naming: #驼峰命名不会自动转化 需配置该项
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
运行效果
🍨 Repsitory接口
在上述案例中,我们除了可以使用JpaRepository接口外还可以使用Repository
接口和CrudRepository
接口
Repostory接口只支持查询语句 有两种查询方式
🌱Repository基于方法名称命名规则查询
public interface UserDaoRepository extends Repository<Users,Integer> {
//IS 就是SQL中的is
List<Users> findByUserNameIs(String string);
//Like
List<Users> findByUserNameLike(String username);
//多条件
List<Users> findByUserNameAndRealNameIs(String name , String realName);
}
测试代码
public void query1(){
List<Users> list = this.dao.findByUserNameIs( string: "Tom");
for (Users users : list) {
system.out.println(users);
}
}
public void query2(){
List<Users> list = this.dao.findByUserNameLike(" Tom");
for (Users users : list) {
System.out.println(users);
}
}
public void query3(){
List<Users> list = this.dao.findByUserNameAndRealNameIs( name:"Tom" ,realName:"张三丰");
for (Users users : list) {
system.out.println(users);
}
}
🌱 基于@Query查询
🍇 JPQL语句
@Query(value = " from Users where userName =?")
List<Users> queryUsersByNameUseJPQL(String name);
@Query(value = " from Users where userName like_?")
List<Users> queryUserLikeNameUseJPQL(String name);
@Query(value = " from Users where userName= ? and realName =?")
List<Users> queryUserByNameAndRealName(String userName ,String realName);
🍇SQL语句
//在使用@Query注解查询SQL语句的时候 nativeQuery默认是false,我们需要设置为true
@Query(value = "select * from t_user where user_name=? " , nativeQuery = true)
List<Users> queryUsersByNameUsesQL(String name);
@Query(value = "select * from t_user where user_name like ?" ,nativeQuery = true)
List<Users> queryUserLikeNameUsesQL(String name);
@Query(value = " from Users where userName= ? and realName = ?")
List<Users> queryUserByNameAndReaLName(String userName , string realName);
若要强行进行DML操作,那么需要加上 @Modifying 注解
🍨 CrudRepository
该接口继承于Repository接口,且又扩展了CRUD的相关操作
public interface UserDaoCrudRepository extends CrudRepository<Users,Integer>{
}
//添加
@Test
public void test1(){
Users user = new Users();
user.setRealName("成龙");
user.setUserName ("chengnong");
//接口中的方法,直接调用即可
dao.save(user);
}
//查询
public void test2(){
User user =dao.findOne(25);
}
//查询多条
public void test3(){
Iterable<Users> list = dao.findAll();
Iterator<Users> iterator = list.iterator();
while(iterator.hasNext()){
Users user = iterator.next();
System.out.println(user);
}
}
🍨pageAndSorting
分页和排序的相关操作
🍰 JpaRepository
其中的方法
🍰JpaSpecificcation
JpaSpecificcationExecutor 可以实现带条件的分页和排序
案例:查询id大于5的记录
关联查询
https://zhuanlan.zhihu.com/p/108016975
JHipster
https://www.cnblogs.com/demingblog/p/13805484.html