Java学习笔记-Day82 Spring Boot框架(二)
一、Spring Boot的注解
(1)@SpringBootApplication:用于标识Spring Boot应用程序,代表当前类是一个Spring Boot启动类,而一个Spring Boot项目内有且只能有一个@SpringBootApplication注解存在。Spring Boot运行这个类的main方法时就会启动SpringBoot应用程序。
(2)@SpringBootConfiguration: Spring Boot的配置类,标注在类上表示当前类是一个Spring Boot的配置类。
(3)@Configuration:标注在类上表示当前类是一个配置类。配置类相当于配置文件。配置类也是容器中的一个组件。
(4)@Component:把组件实例化到Spring容器中。
(5)@EnableAutoConfiguration:开启自动配置功能。当我们需要Spring Boot帮我们自动配置所需要的配置,@EnableAutoConfiguration告诉Spring Boot开启自动配置功能,这时Spring Boot会自动配置好,并使之生效。
(6)@AutoConfigurationPackage:自动配置包。
(7)@Import: Spring的底层注解,导入一个组件到容器中。
-
@Import(AutoConfigurationPackages.Registrar.class):由AutoConfigurationPackages.Registrar.class将主配置类(标注@SpringBootApplication注解的类)的所在目录的包及下面所有子包里面的所有组件扫描到Spring容器。
-
@Import(EnableAutoConfigurationImportSelector.class) :EnableAutoConfigurationImportSelector(组件的选择器) 将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中。组件的选择器给容器中导入非常多的自动配置类(xxxAutoConfiguration),给容器中导入这个场景需要的所有组件,并配置好这些组件。
(9)@ComponentScan:根据定义的扫描路径,将符合扫描规则的类装配到Spring容器中。
二、自定义banner
在Spring Boot项目的 src/main/resources 文件夹中创建一个banner.txt文件,写入ASCII字符画,就能替换默认的banner。
- banner.txt
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永无BUG 永不修改 //
三、Spring Boot整合SSM框架集
(1)点击 New -> 点击 project -> 选择Spring Intializr -> 选择项目的Project SDK -> Choose starter service URL选择Default(https://start.spring.io)-> 点击Next。
(2)修改Java Version为对应的8版本(jdk1.8)。
(3)Web选择Spring Web,SQL选择Mybatis Framework和MySQL Driver,点击Next。
(4)输入项目名称和项目路径,点击Finish完成。
(5)在src目录下创建com.etc.ssm.entity包,并在该包下创建实体类。
- User.java
package com.etc.ssm.entity;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer userid;
private String username;
private String userphone;
private String userpwd;
private Integer userage;
private Integer usersex;
private Date userregistertime;
private Integer ustatus;
private String userpictureurl;
private static final long serialVersionUID = 1L;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserphone() {
return userphone;
}
public void setUserphone(String userphone) {
this.userphone = userphone;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public Integer getUserage() {
return userage;
}
public void setUserage(Integer userage) {
this.userage = userage;
}
public Integer getUsersex() {
return usersex;
}
public void setUsersex(Integer usersex) {
this.usersex = usersex;
}
public Date getUserregistertime() {
return userregistertime;
}
public void setUserregistertime(Date userregistertime) {
this.userregistertime = userregistertime;
}
public Integer getUstatus() {
return ustatus;
}
public void setUstatus(Integer ustatus) {
this.ustatus = ustatus;
}
public String getUserpictureurl() {
return userpictureurl;
}
public void setUserpictureurl(String userpictureurl) {
this.userpictureurl = userpictureurl;
}
public User(String username, String userphone, String userpwd, Integer userage, Integer usersex,
Date userregistertime, Integer ustatus, String userpictureurl) {
super();
this.username = username;
this.userphone = userphone;
this.userpwd = userpwd;
this.userage = userage;
this.usersex = usersex;
this.userregistertime = userregistertime;
this.ustatus = ustatus;
this.userpictureurl = userpictureurl;
}
public User(String username, String userphone, String userpwd, Integer userage, Integer usersex,
String userpictureurl) {
super();
this.username = username;
this.userphone = userphone;
this.userpwd = userpwd;
this.userage = userage;
this.usersex = usersex;
this.userpictureurl = userpictureurl;
}
public User(Integer userid, String username, String userphone, String userpwd, Integer userage, Integer usersex,
String userpictureurl) {
super();
this.userid = userid;
this.username = username;
this.userphone = userphone;
this.userpwd = userpwd;
this.userage = userage;
this.usersex = usersex;
this.userpictureurl = userpictureurl;
}
public User() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", userid=").append(userid);
sb.append(", username=").append(username);
sb.append(", userphone=").append(userphone);
sb.append(", userpwd=").append(userpwd);
sb.append(", userage=").append(userage);
sb.append(", usersex=").append(usersex);
sb.append(", userregistertime=").append(userregistertime);
sb.append(", ustatus=").append(ustatus);
sb.append(", userpictureurl=").append(userpictureurl);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
- UserExample .java
package com.etc.ssm.entity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UserExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public UserExample() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List<Criterion> getAllCriteria() {
return criteria;
}
public List<Criterion> getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andUseridIsNull() {
addCriterion("userid is null");
return (Criteria) this;
}
public Criteria andUseridIsNotNull() {
addCriterion("userid is not null");
return (Criteria) this;
}
public Criteria andUseridEqualTo(Integer value) {
addCriterion("userid =", value, "userid");
return (Criteria) this;
}
public Criteria andUseridNotEqualTo(Integer value) {
addCriterion("userid <>", value, "userid");
return (Criteria) this;
}
public Criteria andUseridGreaterThan(Integer value) {
addCriterion("userid >", value, "userid");
return (Criteria) this;
}
public Criteria andUseridGreaterThanOrEqualTo(Integer value) {
addCriterion("userid >=", value, "userid");
return (Criteria) this;
}
public Criteria andUseridLessThan(Integer value) {
addCriterion("userid <", value, "userid");
return (Criteria) this;
}
public Criteria andUseridLessThanOrEqualTo(Integer value) {
addCriterion("userid <=", value, "userid");
return (Criteria) this;
}
public Criteria andUseridIn(List<Integer> values) {
addCriterion("userid in", values, "userid");
return (Criteria) this;
}
public Criteria andUseridNotIn(List<Integer> values) {
addCriterion("userid not in", values, "userid");
return (Criteria) this;
}
public Criteria andUseridBetween(Integer value1, Integer value2) {
addCriterion("userid between", value1, value2, "userid");
return (Criteria) this;
}
public Criteria andUseridNotBetween(Integer value1, Integer value2) {
addCriterion("userid not between", value1, value2, "userid");
return (Criteria) this;
}
public Criteria andUsernameIsNull() {
addCriterion("username is null");
return (Criteria) this;
}
public Criteria andUsernameIsNotNull() {
addCriterion("username is not null");
return (Criteria) this;
}
public Criteria andUsernameEqualTo(String value) {
addCriterion("username =", value, "username");
return (Criteria) this;
}
public Criteria andUsernameNotEqualTo(String value) {
addCriterion("username <>", value, "username");
return (Criteria) this;
}
public Criteria andUsernameGreaterThan(String value) {
addCriterion("username >", value, "username");
return (Criteria) this;
}
public Criteria andUsernameGreaterThanOrEqualTo(String value) {
addCriterion("username >=", value, "username");
return (Criteria) this;
}
public Criteria andUsernameLessThan(String value) {
addCriterion("username <", value, "username");
return (Criteria) this;
}
public Criteria andUsernameLessThanOrEqualTo(String value) {
addCriterion("username <=", value, "username");
return (Criteria) this;
}
public Criteria andUsernameLike(String value) {
addCriterion("username like", value, "username");
return (Criteria) this;
}
public Criteria andUsernameNotLike(String value) {
addCriterion("username not like", value, "username");
return (Criteria) this;
}
public Criteria andUsernameIn(List<String> values) {
addCriterion("username in", values, "username");
return (Criteria) this;
}
public Criteria andUsernameNotIn(List<String> values) {
addCriterion("username not in", values, "username");
return (Criteria) this;
}
public Criteria andUsernameBetween(String value1, String value2) {
addCriterion("username between", value1, value2, "username");
return (Criteria) this;
}
public Criteria andUsernameNotBetween(String value1, String value2) {
addCriterion("username not between", value1, value2, "username");
return (Criteria) this;
}
public Criteria andUserphoneIsNull() {
addCriterion("userphone is null");
return (Criteria) this;
}
public Criteria andUserphoneIsNotNull() {
addCriterion("userphone is not null");
return (Criteria) this;
}
public Criteria andUserphoneEqualTo(String value) {
addCriterion("userphone =", value, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneNotEqualTo(String value) {
addCriterion("userphone <>", value, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneGreaterThan(String value) {
addCriterion("userphone >", value, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneGreaterThanOrEqualTo(String value) {
addCriterion("userphone >=", value, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneLessThan(String value) {
addCriterion("userphone <", value, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneLessThanOrEqualTo(String value) {
addCriterion("userphone <=", value, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneLike(String value) {
addCriterion("userphone like", value, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneNotLike(String value) {
addCriterion("userphone not like", value, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneIn(List<String> values) {
addCriterion("userphone in", values, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneNotIn(List<String> values) {
addCriterion("userphone not in", values, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneBetween(String value1, String value2) {
addCriterion("userphone between", value1, value2, "userphone");
return (Criteria) this;
}
public Criteria andUserphoneNotBetween(String value1, String value2) {
addCriterion("userphone not between", value1, value2, "userphone");
return (Criteria) this;
}
public Criteria andUserpwdIsNull() {
addCriterion("userpwd is null");
return (Criteria) this;
}
public Criteria andUserpwdIsNotNull() {
addCriterion("userpwd is not null");
return (Criteria) this;
}
public Criteria andUserpwdEqualTo(String value) {
addCriterion("userpwd =", value, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdNotEqualTo(String value) {
addCriterion("userpwd <>", value, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdGreaterThan(String value) {
addCriterion("userpwd >", value, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdGreaterThanOrEqualTo(String value) {
addCriterion("userpwd >=", value, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdLessThan(String value) {
addCriterion("userpwd <", value, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdLessThanOrEqualTo(String value) {
addCriterion("userpwd <=", value, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdLike(String value) {
addCriterion("userpwd like", value, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdNotLike(String value) {
addCriterion("userpwd not like", value, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdIn(List<String> values) {
addCriterion("userpwd in", values, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdNotIn(List<String> values) {
addCriterion("userpwd not in", values, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdBetween(String value1, String value2) {
addCriterion("userpwd between", value1, value2, "userpwd");
return (Criteria) this;
}
public Criteria andUserpwdNotBetween(String value1, String value2) {
addCriterion("userpwd not between", value1, value2, "userpwd");
return (Criteria) this;
}
public Criteria andUserageIsNull() {
addCriterion("userage is null");
return (Criteria) this;
}
public Criteria andUserageIsNotNull() {
addCriterion("userage is not null");
return (Criteria) this;
}
public Criteria andUserageEqualTo(Integer value) {
addCriterion("userage =", value, "userage");
return (Criteria) this;
}
public Criteria andUserageNotEqualTo(Integer value) {
addCriterion("userage <>", value, "userage");
return (Criteria) this;
}
public Criteria andUserageGreaterThan(Integer value) {
addCriterion("userage >", value, "userage");
return (Criteria) this;
}
public Criteria andUserageGreaterThanOrEqualTo(Integer value) {
addCriterion("userage >=", value, "userage");
return (Criteria) this;
}
public Criteria andUserageLessThan(Integer value) {
addCriterion("userage <", value, "userage");
return (Criteria) this;
}
public Criteria andUserageLessThanOrEqualTo(Integer value) {
addCriterion("userage <=", value, "userage");
return (Criteria) this;
}
public Criteria andUserageIn(List<Integer> values) {
addCriterion("userage in", values, "userage");
return (Criteria) this;
}
public Criteria andUserageNotIn(List<Integer> values) {
addCriterion("userage not in", values, "userage");
return (Criteria) this;
}
public Criteria andUserageBetween(Integer value1, Integer value2) {
addCriterion("userage between", value1, value2, "userage");
return (Criteria) this;
}
public Criteria andUserageNotBetween(Integer value1, Integer value2) {
addCriterion("userage not between", value1, value2, "userage");
return (Criteria) this;
}
public Criteria andUsersexIsNull() {
addCriterion("usersex is null");
return (Criteria) this;
}
public Criteria andUsersexIsNotNull() {
addCriterion("usersex is not null");
return (Criteria) this;
}
public Criteria andUsersexEqualTo(Integer value) {
addCriterion("usersex =", value, "usersex");
return (Criteria) this;
}
public Criteria andUsersexNotEqualTo(Integer value) {
addCriterion("usersex <>", value, "usersex");
return (Criteria) this;
}
public Criteria andUsersexGreaterThan(Integer value) {
addCriterion("usersex >", value, "usersex");
return (Criteria) this;
}
public Criteria andUsersexGreaterThanOrEqualTo(Integer value) {
addCriterion("usersex >=", value, "usersex");
return (Criteria) this;
}
public Criteria andUsersexLessThan(Integer value) {
addCriterion("usersex <", value, "usersex");
return (Criteria) this;
}
public Criteria andUsersexLessThanOrEqualTo(Integer value) {
addCriterion("usersex <=", value, "usersex");
return (Criteria) this;
}
public Criteria andUsersexIn(List<Integer> values) {
addCriterion("usersex in", values, "usersex");
return (Criteria) this;
}
public Criteria andUsersexNotIn(List<Integer> values) {
addCriterion("usersex not in", values, "usersex");
return (Criteria) this;
}
public Criteria andUsersexBetween(Integer value1, Integer value2) {
addCriterion("usersex between", value1, value2, "usersex");
return (Criteria) this;
}
public Criteria andUsersexNotBetween(Integer value1, Integer value2) {
addCriterion("usersex not between", value1, value2, "usersex");
return (Criteria) this;
}
public Criteria andUserregistertimeIsNull() {
addCriterion("userregistertime is null");
return (Criteria) this;
}
public Criteria andUserregistertimeIsNotNull() {
addCriterion("userregistertime is not null");
return (Criteria) this;
}
public Criteria andUserregistertimeEqualTo(Date value) {
addCriterion("userregistertime =", value, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeNotEqualTo(Date value) {
addCriterion("userregistertime <>", value, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeGreaterThan(Date value) {
addCriterion("userregistertime >", value, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeGreaterThanOrEqualTo(Date value) {
addCriterion("userregistertime >=", value, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeLessThan(Date value) {
addCriterion("userregistertime <", value, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeLessThanOrEqualTo(Date value) {
addCriterion("userregistertime <=", value, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeIn(List<Date> values) {
addCriterion("userregistertime in", values, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeNotIn(List<Date> values) {
addCriterion("userregistertime not in", values, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeBetween(Date value1, Date value2) {
addCriterion("userregistertime between", value1, value2, "userregistertime");
return (Criteria) this;
}
public Criteria andUserregistertimeNotBetween(Date value1, Date value2) {
addCriterion("userregistertime not between", value1, value2, "userregistertime");
return (Criteria) this;
}
public Criteria andUstatusIsNull() {
addCriterion("ustatus is null");
return (Criteria) this;
}
public Criteria andUstatusIsNotNull() {
addCriterion("ustatus is not null");
return (Criteria) this;
}
public Criteria andUstatusEqualTo(Integer value) {
addCriterion("ustatus =", value, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusNotEqualTo(Integer value) {
addCriterion("ustatus <>", value, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusGreaterThan(Integer value) {
addCriterion("ustatus >", value, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusGreaterThanOrEqualTo(Integer value) {
addCriterion("ustatus >=", value, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusLessThan(Integer value) {
addCriterion("ustatus <", value, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusLessThanOrEqualTo(Integer value) {
addCriterion("ustatus <=", value, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusIn(List<Integer> values) {
addCriterion("ustatus in", values, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusNotIn(List<Integer> values) {
addCriterion("ustatus not in", values, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusBetween(Integer value1, Integer value2) {
addCriterion("ustatus between", value1, value2, "ustatus");
return (Criteria) this;
}
public Criteria andUstatusNotBetween(Integer value1, Integer value2) {
addCriterion("ustatus not between", value1, value2, "ustatus");
return (Criteria) this;
}
public Criteria andUserpictureurlIsNull() {
addCriterion("userpictureurl is null");
return (Criteria) this;
}
public Criteria andUserpictureurlIsNotNull() {
addCriterion("userpictureurl is not null");
return (Criteria) this;
}
public Criteria andUserpictureurlEqualTo(String value) {
addCriterion("userpictureurl =", value, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlNotEqualTo(String value) {
addCriterion("userpictureurl <>", value, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlGreaterThan(String value) {
addCriterion("userpictureurl >", value, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlGreaterThanOrEqualTo(String value) {
addCriterion("userpictureurl >=", value, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlLessThan(String value) {
addCriterion("userpictureurl <", value, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlLessThanOrEqualTo(String value) {
addCriterion("userpictureurl <=", value, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlLike(String value) {
addCriterion("userpictureurl like", value, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlNotLike(String value) {
addCriterion("userpictureurl not like", value, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlIn(List<String> values) {
addCriterion("userpictureurl in", values, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlNotIn(List<String> values) {
addCriterion("userpictureurl not in", values, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlBetween(String value1, String value2) {
addCriterion("userpictureurl between", value1, value2, "userpictureurl");
return (Criteria) this;
}
public Criteria andUserpictureurlNotBetween(String value1, String value2) {
addCriterion("userpictureurl not between", value1, value2, "userpictureurl");
return (Criteria) this;
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table t_user
*
* @mbg.generated do_not_delete_during_merge Wed Mar 10 10:47:38 CST 2021
*/
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
(6)在src目录下创建com.etc.ssm.dao包,并在该包下创建Dao类。
package com.etc.ssm.dao;
import java.util.List;
import com.etc.ssm.entity.User;
import com.etc.ssm.entity.UserExample;
import org.springframework.stereotype.Repository;
@Repository
//Mapper
public interface UserMapper {
List<User> selectByExample(UserExample example);
}
(7)在resource目录下创建mapper文件夹,并在该文件夹中创建xxxMapper.xml文件。
- UserMapper.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.etc.ssm.dao.UserMapper">
<resultMap id="BaseResultMap" type="com.etc.ssm.entity.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed Mar 10 10:47:38 CST 2021.
-->
<id column="userid" jdbcType="INTEGER" property="userid" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="userphone" jdbcType="VARCHAR" property="userphone" />
<result column="userpwd" jdbcType="VARCHAR" property="userpwd" />
<result column="userage" jdbcType="INTEGER" property="userage" />
<result column="usersex" jdbcType="INTEGER" property="usersex" />
<result column="userregistertime" jdbcType="TIMESTAMP" property="userregistertime" />
<result column="ustatus" jdbcType="INTEGER" property="ustatus" />
<result column="userpictureurl" jdbcType="VARCHAR" property="userpictureurl" />
</resultMap>
<sql id="Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed Mar 10 10:47:38 CST 2021.
-->
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed Mar 10 10:47:38 CST 2021.
-->
userid, username, userphone, userpwd, userage, usersex, userregistertime, ustatus,
userpictureurl
</sql>
<select id="selectByExample" parameterType="com.etc.ssm.entity.UserExample" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed Mar 10 10:47:38 CST 2021.
-->
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from t_user
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
</mapper>
(8)配置applicationContext.xml文件。
- applicationContext.xml
#修改tomcat的端口
server.port=8080
#数据库连接配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bookdb?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#指定mybatis中配置的映射文件的地址
mybatis.mapper-locations=classpath:/mapper/*.xml
##日志配置
logging.level.com.etc.ssm.dao=DEBUG
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
(9)在test/java/com/etc/ssm目录下创建测试类。
package com.etc.ssm;
import com.etc.ssm.dao.UserMapper;
import com.etc.ssm.entity.UserExample;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoadsAll() {
UserExample userExample = new UserExample();
userExample.createCriteria().andUsernameLike("%%");
System.out.println(userMapper.selectByExample(userExample));
}
}
运行测试类的结果如下:
(10)在src目录下创建com.etc.ssm.service包,并在该包下创建Service类。
- UserService.java
package com.etc.ssm.service;
import com.etc.ssm.entity.User;
import java.util.List;
public interface UserService {
public List<User> getUserAll();
}
(11)在src目录下创建com.etc.ssm.service.impl包,并在该包下创建Service实现类。
- UserServiceImpl.java
package com.etc.ssm.service.impl;
import com.etc.ssm.dao.UserMapper;
import com.etc.ssm.entity.User;
import com.etc.ssm.entity.UserExample;
import com.etc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public List<User> getUserAll() {
UserExample userExample = new UserExample();
userExample.createCriteria().andUsernameLike("%%");
return userMapper.selectByExample(userExample);
}
}
(12)在src目录下创建com.etc.ssm.controller包,并在该包下创建Controller实现类。
package com.etc.ssm.controller;
import com.etc.ssm.entity.User;
import com.etc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("api/user/all")
public List<User> getUser(){
return userService.getUserAll();
}
}
(13)在src/com/etc/ssm目录下创建DemoApplication.java文件(Spring Boot启动类)。
package com.etc.ssm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.etc.ssm.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
运行DemoApplication.java文件,启动Spring Boot项目。
四、@Mapper和@Repository在Dao层的应用
@Mapper是mybatis自身带的注解,使用@Mapper后,不需要在spring配置中设置扫描包地址,而是通过mapper.xml里面的namespace属性对应相关的mapper类,Spring 将动态生成Bean,并将其注入到Service层。
package com.etc.ssm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Repository是Spring的注解,需要在Spring中配置扫描包地址,然后生成Dao层的bean,之后将其注入到Service层。如果使用@Repository注解,则Spring Boot程序启动入口(DemoApplication类)需要加入@MapperScan(“comm.etc.ssm.dao”)。
package com.etc.ssm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.etc.ssm.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
五、Thymeleaf模板引擎
Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎,类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。
Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
操作步骤:
(1)在pom.xml添加thymeleaf依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
(2)在applicationContexy.xml文件中配置thymeleaf。
#修改tomcat的端口
server.port=8080
#数据库连接配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bookdb?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#指定mybatis中配置的映射文件的地址
mybatis.mapper-locations=classpath:/mapper/*.xml
##日志配置
logging.level.com.etc.ssm.dao=DEBUG
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
##thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=utf-8
(3)将html文件放在\src\main\resources\templates目录中,在html页面中引入thymeleaf命名空间,即此时在html模板文件中动态的属性使用th:命名空间修饰。
<html lang="en" xmlns:th="http://www.thymeleaf.org">
(4)使用thymeleaf语法渲染数据。
- index.xml
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="10">
<tr>
<th>用户编号</th>
<th>用户名称</th>
<th>用户电话</th>
<th>用户年龄</th>
</tr>
<tr th:each="user : ${userlist}">
<td th:text="${user.userid}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.userphone}"></td>
<td th:text="${user.userage}"></td>
</tr>
</table>
</body>
- UserController.java
package com.etc.ssm.controller;
import com.etc.ssm.entity.User;
import com.etc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class UserController {
@Autowired
UserService userService;
@GetMapping("api/user/all")
public String getUserAll(Model model){
List<User> userlist = userService.getUserAll();
model.addAttribute("userlist",userlist);
return "index";
}
}
六、端口被占用的解决方法
使用cmd进入DOS命名窗口,使用命令查看占用端口的进程号,通过进程号关闭该进程,释放端口。
查看占用端口的进程号:netstat -ano | findstr 8080
关闭进程:taskkill /pid 4908 -t -f