0831 summary
The first is talked about some of the basic foundation of Java, then that is their practical realization of a collection from the class definition, in this collection classes, like the realization of the same list, there is the initial length (size), when the length is more than a collection of the initial length of the collection class, to be expanded length (e.g. ArrayList expansion is expanded to 1.5 times, Vector is expanded to 2 times the original), must add function, GET functions are actually implemented interfaces ;
package peixun;
import java.util.Collection;
import java.util.Iterator;
java.util.Spliterator import;
import java.util.Spliterators;
public interface settest<E> extends Collection<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean retainAll(Collection<?> c);
boolean removeAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.DISTINCT);
}
}
Then explain that a bit of the current framework technology (companies with more is SpringBoot, SSH, SSM) personal understanding, or SpringBoot better use some, but can not say that it will only use their own feel good, the rest will not, so even if he will be very front-end technology, but the technology used and the company's main inconsistency, inevitably there will be problems at work, so, for our students, we have to grasp a lot, as long as it can with something, we have to learn, here, to make a personal collate and summarize the framework for SSM:
SSM is the combination SpringMVC, Spring and Mybatis of.
SpringMVC: It is used for the web layer corresponds Controller (equivalent to the conventional and the struts servlet Action), to process the user request. Before learned SpringBoot which is then analyzed by the controller of the user's request and returns a string (the string is usually the name of the page you want to return, because the prefixes and suffixes in our framework have been defined, of course, himself can be modified custom) in the configuration file, then the string will lead us to the page you want us to go back to one of our designated.
E.g:
@Controller
@RequestMapping("/book")
public class BookController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private BookService bookService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
private String list(Model model) {
List<Book> list = bookService.getList();
model.addAttribute("list", list);
// list.jsp + model = ModelAndView
return "list";// WEB-INF/jsp/"list".jsp
}
Here, in fact, we have by sending a request (this request is that list, that is, our visit is the local host address bar: // 8080 / list), then the request will be through our program, our Controller it is resolved, and there may be a series of operations related function or operation and so on, then you can return a string type of string that is what we want to address our visit, of course, prefixes and suffixes splicing when necessary, so as to access to the page we want to go,
Spring: too strong, so I can not use a word or phrase to summarize it. But we usually estimate the development of the contact is the most IOC containers, which can be loaded bean (that is, we java in the class, of course, including service dao inside), with this mechanism, we do not use this in every class when it is initialized, rarely see the keyword new. The additional spring aop, transaction management, and so are we often use.
MyBatis: personally feel better with MyBatis compared to Hibernate. First, it can be freely controlled sql, the code which will have a database prepared by experienced people can do to enhance the efficiency of database access. Second, it can use the xml way to manage our organization sql, because the general procedural error in many cases sql error, others took over after the code can quickly find wrong place, or even the original can be optimized write sql. Since the first systematic study of the individual is SpringBoot, SpringBoot is generally the integration of these, inside the framework of these mechanisms, in SpringBoot in both, MyBatis for my personal understanding is a series of operations on the database, then we do not like as the beginning of each operation must be written to the database related methods, so that when the project is running, will be more tired, because this should also be developed on MyBatis, find the problem is more convenient, but also simple.
For example: In our dao which has such a function code:
Appointment queryByKeyWithBook(@Param("bookId") long bookId, @Param("studentId") long studentId);
In it is the name of the function queryByKeyWithBook, and has passed the relevant parameters in the back, is a is a bookid syudentid, then it will be implementation-dependent in our method of operating a mapper in:
<select id="queryByKeyWithBook" resultType="Appointment">
<! - tell MyBatis how to map the results to a map book while Appointment Properties ->
<! - free to control SQL ->
SELECT
a.book_id,
a.student_id,
a.appoint_time,
b.book_id "book.book_id",
b.`name` "book.name",
b.number "book.number"
FROM
appointment a
INNER JOIN book b ON a.book_id = b.book_id
WHERE
a.book_id = #{bookId}
AND a.student_id = #{studentId}
</select>
This is called our operating methods, and then we will return value to Appointment types return, of course, Appointment is our entity class a.
Second, our framework is no longer used jdbc 'way connected to a database, and now we have the link to the database is basically all done in the configuration file;
Ssm e.g. jdbc.properties herein in:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/340message?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
There are links to the database in SpringBoot is also full realization in the database:
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.2.108:3306/mybatis
driver-class-name: com.mysql.cj.jdbc.Driver
Again summarize the main level of the architecture ssm:
For our directory levels:
Dao is the data access layer (interfaces) to deal with data, can be database operations, file read and write operations can be even redis cache operation, in short, all the data related to the operation on here, it was also called the dal or data persistence layer almost all meaning. We use the mybatis, so each interface method can be implemented directly in the configuration file.
Entity is the entity class, the general database corresponding to the appearances, dao encapsulating layer is taken out of a data object is, we often say POJO, generally only between dao transfer layer and the service layer.
Dto is the data transport layer, and a lot of time an entity can not meet our business needs, the information may be presented to the user very much, this time there will be a dto, also equivalent to vo, do not mix in this entity inside
Service is the business logic (interfaces), there is to write our business logic, it was also called bll, in the design of service interfaces should stand when the "user" perspective.
serviceImpl is the business logic (realized), to achieve our business interface, control GS is written here.
Web is the controller, springmvc is here to play a role, also called controller controller, the equivalent of struts in action.
In mapper in grammatical structure is:
<?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.soecode.lyf.dao.BookDao">
<- Objective:! To dao provide interface methods sql statement Configuration ->
<select id="queryById" resultType="Book" parameterType="long">
<-! Specific SQL ->
SELECT
book_id,
name,
number
FROM
book
WHERE
book_id = #{bookId}
</select>
<select id="queryAll" resultType="Book">
SELECT
book_id,
name,
number
FROM
book
ORDER BY
book_id
LIMIT #{offset}, #{limit}
</select>
<update id="reduceNumber">
UPDATE book
SET number = number - 1
WHERE
book_id = #{bookId}
AND number > 0
</update>
</mapper>
Project is a maven, so pom.xml is essential:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.soecode.ssm</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url>http://github.com/liyifeng1994/ssm</url>
<dependencies>
<! - Test Unit ->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<! - 1. Logs ->
<-! Slf4j implement the interface and integration ->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<! - 2. Database ->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<! - DAO: MyBatis ->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 3.Servlet web -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 4.Spring -->
<-! 1) Spring Core ->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 2)Spring DAO层 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 3)Spring web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 4)Spring test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<- redis client:! Jedis ->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency>
<-! Map Tools ->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
</build>
</project>
Configuration in Spring, or more, we can be divided into three levels, that is, dao, service, as well as web that is controller
first step,
1, first of all we want our database of relevant links to read the parameters of the database program,
2, then we are to configure our database connection pool,
3, configure the connection properties, you can not read the configuration file directly here to write dead
4, c3p0 configuration files, configure only a few commonly used on the line
5, the object that is configured mybatis played sessionfactory
6, the scanning dao layer interfaces, sql and parameters are written in xml
Because the database configuration parameters related to the configuration file is read, so the resources
folder create a new jdbc.properties
file, store our four most common database connection properties,
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
Here database operations using a mybatis, we will configure mybatis-config.xml file in recources, the system does not have to configure their own
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<! - Configure Global Properties ->
<settings>
<! - Use of getGeneratedKeys get jdbc database auto-increment primary key ->
<setting name="useGeneratedKeys" value="true" />
<! - use a column alias to replace the column name Default: true ->
<setting name="useColumnLabel" value="true" />
<! - Open camelCasing conversion: Table {create_time} -> Entity {createTime} ->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
Step Two: (service layer configuration), the spring folder New spring-config.xml file
1, the scanning service package all annotations @Service
2, Configuration Manager things, the things to manage to complete the Spring
3, the configuration based on direct declarative things can @Tanscation directly on the method
In the Spring-config.xml configuration file:
<?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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<! - service package scan all annotations using types ->
<context:component-scan base-package="com.soecode.lyf.service" />
<! - Configure Transaction Manager ->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<! - injecting database connection pool ->
<property name="dataSource" ref="dataSource" />
</bean>
<! - Configure annotation-based declarative transaction ->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
The third step: Our web layer is configured, the new Spring-web.xml file folder in the Spring
1, to open SpringMVC annotation mode, you can use @ RequestMapping, @ Responsebody
2, static files are processed, such as js \ css \ jpg, etc.
3, configuration jsp show viewResolver, for example, a method returns a String type of login in controlller in fact returns "WEB-INF / login.jsp", the fact is, prefixes and suffixes splicing
4, @Controller scan web layer
In the Spring-web.xml configuration file:
<?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-3.0.xsd">
<! - Configuration SpringMVC ->
<! - 1. Turn SpringMVC annotation mode ->
! <- simplify configuration:
(1) automatic registration DefaultAnootationHandlerMapping, AnotationMethodHandlerAdapter
(2) provide some columns: format @NumberFormat data binding, numbers and dates, @DateTimeFormat, xml, json default read-write support
-->
<mvc:annotation-driven />
<! - default servlet 2. configure a static resource
(1) added to the treatment of static resources: js, gif, png
(2) allows the use of "/" do the whole map
-->
<mvc:default-servlet-handler/>
<! - Display Configuration jsp ViewResolver ->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<! - 4. Scan web associated bean ->
<context:component-scan base-package="com.soecode.lyf.web" />
</beans>
The fourth step: to create and configure web.xml file exists in the web-inf under
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 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_3_1.xsd"
version="3.1" metadata-complete="true">
<! - If you are using xml mvn command generated need to modify the servlet version 3.1 ->
<! - Configuration DispatcherServlet ->
<servlet>
<servlet-name>seckill-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<! - Configure springMVC need to load configuration file
spring-dao.xml,spring-service.xml,spring-web.xml
Mybatis - > spring -> springmvc
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>seckill-dispatcher</servlet-name>
<! - default match all requests ->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Configuring logging, create logback.xml in the resources folder
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>