因为要准备明天面试(早点呼呼,看看翻译工具)所以今天也先占个位置噢
Spring
优势:
方便解耦,简化开发
AOP切面编程
声明事务
整合各种优秀的框架
Spring官网:
https://spring.io
可以下载所需的jar包
Spring生态系统图
SpringFramework核心模块结构图
Spring Ioc控制反转 (DI 依赖注入)
【说明解释】
前端 java后端程序 数据库
java后端程序:
控制层(获取前端数据,控制页面跳转,调用业务层)Controller接口、ControllerImpl实现
业务层(处理具体的业务,调用数据库连接层)Service接口、ServiceImpl实现
数据库连接层(数据库连接以及增删改查等具体的操作)Mapper接口、MapperImpl实现
Spring Ioc控制反转:
applicationContext.xml定义各种对象反射 > Spring容器放各种对象(键值对) > java后端程序
Ioc代码实现
【添加依赖】
导入spring-context(他实际包含6个包)
【配置cpplicationContext.xml】
在beans下添加(可以从官网抄)
<bean id="id" class="路径/对象类"></bean>
【新建测试类创建Spring容器】
//可以从官网抄
Application Context context = new ClassPathXmlApplicationContext(cpplicationContext.xml);
//获取对象
对象类 对象变量名=context.get对象类名("xml里面定义的对象类名");
属性注入
【设值注入】
在bean里面加入,name跟set后面的名字有关系,value基本类型,ref类
<property name="xxx" value="xxx"><property>
<property name="yyy" ref="yyy"><property>
yyy在上面定另一个bean的id
【构造注入】
在bean里面加入,name跟构造器参数名字有关系
<constructor-arg name="xxx" value="xxx"><constructor-arg>
注解的使用
@xxxx,未来趋向于:框架=注解+反射+设计模式
比如:
加在对象类上面的@Component他会帮我们创建对象(没有初始化值)
在具体的普通属性上面加上@value("值"),不依赖set方法
在具体的类属性上面加上@Autowired,自动注入进来,不依赖set方法
之后在applicationContext.xml里面添加命名空间和aop约束并改aop为context
去官网找aop约束(3条)
命名空间:<context:compinent-scan base-package="放对象类的包"></context:compinent-scan>
SpringMVC
创建web项目,配置pom.xml文件(添加spring依赖和tomcat插件)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/solicitous4</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
之后在src>main下面添加java目录,创建bean目录和controller目录。
bean目录里面放实体类,例如:
public class First {
int a;
String b;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
controller里面放带@RequestMapping("")注解,返回响应的函数(不应包含业务逻辑),例如:
@RequestMapping("/aa")
public String test(First first){
System.out.println(first.getA()+"---"+first.getB());
return "a.jsp";
}
在src>main下面添加resources目录,里面添加springmvc.xml,之后添加解析的标签信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描控制器类,千万不要把service等扫进来,也千万不要在Spring配置文件扫描控制器类所在包 -->
<context:component-scan base-package="controller的包路径"></context:component-scan>
<!-- Spring MVC的注解生效:RequestMapping -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
配置src>webapp>WEB-INF里的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--springmvc.xml 名称自定义,只要和我们创建的配置文件的名称对应就可以了。-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--Tomcat启动立即加载Servlet,而不是等到访问Servlet才去实例化DispatcherServlet-->
<!--配置上的效果:Tomcat启动立即加载SpringMVC框架的配置文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/表示除了.jsp结尾的uri,其他的uri都会触发DispatcherServlet。此处千万不要写成/*-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
点击maven找到对应的tomcat的run双击运行
ssh
创建web项目,配置pom.xml文件(添加spring依赖和tomcat插件)
注意:这里需要使用tomcat8
<?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>org.example</groupId>
<artifactId>solicitous5SSH</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.19</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
</dependencies>
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.tomcat.maven</groupId>-->
<!-- <artifactId>tomcat7-maven-plugin</artifactId>-->
<!-- <version>2.2</version>-->
<!-- <configuration>-->
<!-- <path>/solicitous5SSH</path>-->
<!-- <port>8080</port>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<pluginRepositories>
<pluginRepository>
<id>mvnrepository</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
<version>3.0-r1756463</version>
<configuration>
<path>/solicitous5SSH</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
补充修改目录,添加对应的文件内容
src/ main/ java/ com/ solicitous/ controller/ ← 控制器 service/ ← 服务层 repository/ ← 数据访问层(或dao) entity/ ← 实体类(推荐)也有用mode、popj、bean、domain……的 util/ ← 工具类 config/ ← 配置类 resources/ mapper/ ← MyBatis映射文件 static/ ← 静态资源 templates/ ← 模板文件
controller下面添加文件
package com.solicitous.controller;
import com.solicitous.entity.MyXJZ;
import com.solicitous.service.MyXJZService;
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 java.util.List;
@Controller
//@RestController
public class MyXJZController {
@Autowired
private MyXJZService myXJZService;
@RequestMapping(value = "/findMyXJZ",produces = "text/html;charset=utf-8")
@ResponseBody
public String findAll(){
List<MyXJZ> list=myXJZService.selectXJZ();
String str="";
System.out.println("找到"+list.size()+"个小夹子");
for(int i=0;i<list.size();i++){
str=str+list.get(i).getName();
}
return "hello "+str;
}
}
service下面创建一个impl包放具体的实现
在service下面添加文件,这是一个接口
package com.solicitous.service;
import java.util.List;
public interface MyXJZService {
List selectXJZ();
}
之后再impl里面添加具体的实现类
package com.solicitous.service.impl;
import com.solicitous.repository.MyXJZMapper;
import com.solicitous.service.MyXJZService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MyXJZServiceImpl implements MyXJZService {
@Autowired
private MyXJZMapper myXJZMapper;
public List selectXJZ(){
return myXJZMapper.selectAll();
}
}
在repository下面增加接口
package com.solicitous.repository;
import com.solicitous.entity.MyXJZ;
import java.util.List;
public interface MyXJZMapper {
List<MyXJZ> selectAll();
}
在entity下面添加,和数据库对应的实体类
package com.solicitous.entity;
public class MyXJZ {
private int id;
private String name;
private double age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
}
接下来是在resources目录下面添加配置目录和文件
我这里添加了com.solicitous.mapper目录,这里面添加MyXJZMapper.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.solicitous.repository.MyXJZMapper">
<select id="selectAll" resultType="com.solicitous.entity.MyXJZ">
select * from my_xjz
</select>
</mapper>
在resources目录下面添加applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 连接数据库,获取数据源,配置数据源,设置数据库连接的四个参数 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 利用setter方法完成属性注入,四个参数名固定的,注意源码中虽然没有driverClassName属性,但是有driverClassName的setter方法 -->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/xyjt_test"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- 获取sqlSessionFactory对象 -->
<!-- 以前SqlSessionFactory都是在测试代码中我们自己创建的,现在不用了,整合包 -->
<bean id ="sshFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 给包下类起别名 -->
<property name="typeAliasesPackage" value="com.solicitous.entity"></property>
<!-- 关键配置:指定mapper.xml文件的位置 -->
<property name="mapperLocations" value="classpath*:com/solicitous/mapper/*.xml"/>
<!-- 新加解析的mybatis.xml -->
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
<!-- 扫描mapper文件 -->
<!-- 设置扫描哪个包,进行接口绑定 -->
<!-- 多有Mapper接口代理对象都能创建出来,可以直接从容器中获取出来。 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 和SqlSessionFactory产生联系,以前接口绑定sqlSession.getMapper(XXXMapper.xml)
都是通过以前接口绑定sqlSession来调用Mapper,所以这里一定要注入工厂
注意这里sqlSessionFactoryBeanName类型为String,所以用value把工厂名字写过来就行 -->
<property name="sqlSessionFactoryBeanName" value="sshFactory"></property>
<!--扫描的包 -->
<property name="basePackage" value="com.solicitous.repository"></property>
</bean>
<!-- 扫描包下的注解 -->
<context:component-scan base-package="com.solicitous.service"></context:component-scan>
</beans>
在resources目录下面添加log4j.properties
# 全局日志配置fatal致命的>error错误>warn警告>info普通>debug调试>trace跟踪
log4j.rootLogger=error, console
# MyBatis 日志配置namespace:solicitous1.test2 可以根据自己的写
log4j.logger.com.msb.mapper.MyPojoMapper=TRACE
# 控制台输出
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1}:%L - %m%n
在resources目录下面添加mybatis.xml(还要这个是因为日志需要配置,其他的在applicationContext.xml里面了
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- <!– 加载属性文件,当前文件和db.properties在同一个目录中,使用相对路径 –>-->
<!-- <properties resource="db.properties"></properties>-->
<!-- 位置为properties下面,typeAliases上面,所以 の具体可以ctrl+点击mybatis-3-config.dtd查看 -->
<settings>
<!-- 配置生效的日志 -->
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- <!– 别名 –>-->
<!-- <typeAliases>-->
<!-- <!– type类型全限定路径 alias:别名名称 ,可以和下面的包二选一 –>-->
<!-- <typeAlias type="com.pojo.MyPojo" alias="s"></typeAlias>-->
<!-- <!– 指定包,可以和上面的typeAlias二选一 –>-->
<!-- <package name="com.pojo"/>-->
<!-- </typeAliases>-->
<!-- <environments default="mysql">-->
<!-- <!– 连接mysql数据库的数据库源配置 –>-->
<!-- <environment id="mysql">-->
<!-- <!– 事务处理器的配置 jdbc、manage–>-->
<!-- <transactionManager type="JDBC"></transactionManager>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="${driver}"/>-->
<!-- <property name="url" value="${url}"/>-->
<!-- <property name="username" value="${username}"/>-->
<!-- <property name="password" value="${password}"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
<!-- </environments>-->
<!-- -->
<!-- <mappers>-->
<!-- <mapper resource="mapper\MyPojo.xml"></mapper>-->
<!-- <package name="mapper"/>-->
<!-- </mappers>-->
</configuration>
在resources目录下面添加springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描控制器类,千万不要把service等扫进来,也千万不要在Spring配置文件扫描控制器类所在包 -->
<context:component-scan base-package="com.solicitous.controller"></context:component-scan>
<!-- Spring MVC的注解生效:RequestMapping -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
在webapp的WEB-INF目录下的web.xml里设置
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<servlet>
<servlet-name>ssh</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 参数名称必须叫做:contextConfigLocation。单词和大小写错误都导致配置文件无法正常???-->
<param-name>contextConfigLocation</param-name>
<!--springmvc.xml 名称自定义,只要和我们创建的配置文件的名称对应就可以了。-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--Tomcat启动立即加载Servlet,而不是等到访问Servlet才去实例化DispatcherServlet-->
<!--配置上的效果:Tomcat启动立即加载SpringMVC框架的配置文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssh</servlet-name>
<!--/表示除了.jsp结尾的uri,其他的uri都会触发DispatcherServlet。此处千万不要写成/*-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 新加入 -->
<!-- 解析applicationContest.xml:利用监听器监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 给全局参数 contextConfigLocation设置值,contextConfigLocation是??? -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
点击项目,点击maven找到tomcat8,双击tomcat8:run-war