1 Review
1.1 Benefits of Maven
Save space and manage jar packages in a unified way
One button construction
Cross platform
Application in large-scale projects can improve development efficiency
1.2 Maven Installation Deployment Configuration
1.3 Maven's warehouse
Local warehouse
Remote Warehouse (Private Service)
Central repository
1.4 Adding dependencies
Search from the Internet:
http://mvnrepository.com/(Recommendation)
Reconstruct the index locally and search it indexedly
1.5 Project Construction
1.6 Dependency Scope
Compile struts 2 framework jar
Provided. jsp-api.jar (because he will conflict with tomcat's package)
Runtime Database Driver Package
Test junit.jar
1.7 summary
<modelVersion>
Coordinate GAV
<groupId>cn.itcast</groupId>
<artifactId>ssh</artifactId>
<version>0.0.1-SNAPSHOT</version>
Packaging
Jar war pom
<dependencies>
<dependency>
Build > Inside are plug-ins
<plugins>
<plugin>
2. Integrating ssh Framework
2.1 Dependent Transfer
Only one struts2-core dependency was added and many jar s were found in the project.
This is called dependency passing.
2.2 Resolution of Dependent Version Conflict
1. Principle of Priority of the First Statement
<dependencies> <!-- spring-beans-4.2.4 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- spring-beans-3.0.5 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.24</version> </dependency>
2. The Priority Principle of Nearest Path
Add your own jar package
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.4.RELEASE</version> </dependency>
3. Exclusion Principle
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.24</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </exclusion> </exclusions> </dependency>
4. Version Locking Principle
<properties> <spring.version>4.2.4.RELEASE</spring.version> <hibernate.version>5.0.7.Final</hibernate.version> <struts.version>2.3.24</struts.version> </properties> <!-- Locked version, struts2-2.3.24,spring4.2.4,hibernate5.0.7 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </dependencyManagement>
Demand:
Pass Customer ID Page to Display Customer Information
Preparing database
2.3 Construction Projects
1. Create a database.
2. Execute prepared sql scripts
Location of Sql script:
3. Improving the pom.xml file and adding ssh-related dependencies
<!-- attribute --> <properties> <spring.version>4.2.4.RELEASE</spring.version> <hibernate.version>5.0.7.Final</hibernate.version> <struts.version>2.3.24</struts.version> </properties> <!-- Locked version, struts2-2.3.24,spring4.2.4,hibernate5.0.7 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- Dependency management --> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <!-- Database Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency> <!-- c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- Import struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> </dependency> <!-- servlet jsp --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!-- Journal --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <plugins> <!-- Set the compiled version to 1.7 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- maven Built in tomcat6 Plug-in unit --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <!-- Flexible Engineering Path Configuration --> <path>/ssh</path> <!-- Port number can be configured flexibly --> <port>8080</port> </configuration> </plugin> </plugins> </build>
4. Complete Entity Class Code
public class Customer { private Long custId; private String custName; private Long custUserId; private Long custCreateId; private String custIndustry; private String custLevel; private String custLinkman; private String custPhone; private String custMobile; public Long getCustId() { return custId; } ..ellipsis get set Method
5. Complete dao code
Interface package cn.itcast.dao; import cn.itcast.entity.Customer; public interface CustomerDao { public Customer getById(Long id); } //Implementation class package com.itcast.dao.impl; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import cn.itcast.dao.CustomerDao; import cn.itcast.entity.Customer; public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao { @Override public Customer getById(Long id) { return this.getHibernateTemplate().get(Customer.class, id); } }
Configure spring, add customer bean s to inject sessionFactory, and then right-click the java file for junit testing
6. Complete service code
Interface package com.itcast.service; import cn.itcast.entity.Customer; public interface CustomerService { public Customer getById(Long id); } //Implementation class package com.itcast.service.impl; import com.itcast.service.CustomerService; import cn.itcast.dao.CustomerDao; import cn.itcast.entity.Customer; public class CustomerServiceImpl implements CustomerService { private CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) { this.customerDao = customerDao; } @Override public Customer getById(Long id) { return customerDao.getById(id); } }
7. Complete action code
package cn.itcast.action; import com.itcast.service.CustomerService; import com.opensymphony.xwork2.ActionSupport; import cn.itcast.entity.Customer; public class CutomerAction extends ActionSupport { //Two member variables private Customer customer; private Long custId; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } private CustomerService customerService; public void setCustomerService(CustomerService customerService) { this.customerService = customerService; } public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String findById(){ customer = customerService.getById(custId); return SUCCESS; } }
8. Copy configuration files and modify them
Get the configuration file from the following location
Put it in the src/main/resources directory
A brief revision
9. Modify web.xml to add spring monitoring
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
10. Operating projects
3. Modular development
The Impact of Dependency Scope on Dependency Transfer (Understanding)
Service relies directly on dao (compile by default) and the junit dependency range passed from dao is test, so junit will not be received in service.
Parent Engineering to Manage Aggregation
3.1 Create a parent project:
1,
2. The father project is created as follows
3. Add the following information to pom.Xml:
<!-- attribute --> <properties> <spring.version>4.2.4.RELEASE</spring.version> <hibernate.version>5.0.7.Final</hibernate.version> <struts.version>2.3.24</struts.version> </properties> <!-- Locked version, struts2-2.3.24,spring4.2.4,hibernate5.0.7 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- Dependency management --> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <!-- Database Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency> <!-- c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- Import struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> </dependency> <!-- servlet jsp --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!-- Journal --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <plugins> <!-- Set the compiled version to 1.7 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- maven Built in tomcat6 Plug-in unit --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <!-- Flexible Engineering Path Configuration --> <path>/ssh</path> <!-- Port number can be configured flexibly --> <port>8080</port> </configuration> </plugin> </plugins> </build>
4. Publish to local warehouse
dao service web
3.2 Create dao sub-module
1. Right-click on the ssh-parent project and select Maven Module when creating
2. Fill in the name of the sub-module ssh-dao
3. Copy the code belonging to dao into the module:
4. Publish to local warehouse after completion
3.3 Create service sub-module
1. The way of creation is as follows:
2. Copy the service code into the project
3. Publish to local warehouse
Service Of Junit test //@ContextConfiguration(locations={"classpath*:applicationContext-*.xml"}) First*For the purpose of jar Look in the bag @ContextConfiguration(locations={"classpath:applicationContext-dao.xml","classpath:applicationContext-service.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class CustomerServiceImplTest { // @Test // public void test() { // ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext-dao.xml"); // CustomerDao customerDao = (CustomerDao) app.getBean("customerDao"); // Customer customer = customerDao.findById(23L); // System.out.println("********************"+customer.getCustName()); // } @Autowired private CustomerService customerService; @Test public void test() { // Customer customer = customerService.findById(1L); // System.out.println("********************"+customer.getCustName()); } }
3.4 Create Action Submodule
1. Choose the packaging method of war
2. Copy code and configuration files that belong to action
3. Modify web.xml to add spring listening
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param>
4. Add pages:
4. Private nexus
Install nexus
Startup service
Solutions to startup failure:
Login nexus
User name/password admin/admin123
Warehouse type
Virtual Warehouse
Proxy Agent Warehouse
Hosted Host Warehouse Local Warehouse
Group Group
Demand:
Put dao on private clothes, then service downloads from private clothes
Requirement: Put the project of ssh_dao into jar package and put it into private service.
4.1 Upload dao
The first step is to configure the maven environment on the computer where the dao project is deployed on the client side, modify the settings.xml file, and configure the user and password to connect to the private service.
This username and password is used to verify private service, because private service needs to know whether the account and password of uploaded users are the same as those of private service.
<server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server>
Step 2: Configure the project pom.xml
Configuring the address of the private service warehouse, the company's own jar package will be uploaded to the private service's host warehouse, which host warehouse will be uploaded according to the version decision of the project, if the version is release, to the private service's release warehouse, and if the version is snapshot, to the private service's snapshot warehouse.
<distributionManagement> <repository> <id>releases</id> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>
Note: pom.xml here < ID > corresponds to settings.xml configuration < ID >!
Step 3: Execute the deploy command and publish it to private service
4.2 Download dao
The first step is to modify settings.xml
<profile> <! - ID of profile - > <id>dev</id> <repositories> <repository> <! - Warehouse id, repositories can configure mu lt iple warehouses to ensure that the ID is not duplicated - > <id>nexus</id> <! - Warehouse address, that is, the address of the nexus warehouse group - > <url>http://localhost:8081/nexus/content/groups/public/</url> Whether to download releases components or not <releases> <enabled>true</enabled> </releases> Whether to download snapshots components or not <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <! - Plug-in repository, maven's running dependencies on plug-ins, also need to download plug-ins from private service - > <pluginRepository> The id of the plug-in repository is not allowed to be duplicated. If the configuration is duplicated, it will cover the front - > The id of the plug-in repository is not allowed to be duplicated. <id>public</id> <name>Public Repositories</name> <url>http://localhost:8081/nexus/content/groups/public/</url> </pluginRepository> </pluginRepositories> </profile> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles>
Step 2 Delete dao from the local warehouse
The third step is the update service project. The following information shows that it has been successful
If dao modifies the content during project development, remember to change the version number when re-uploading. Otherwise, service can't be downloaded, because maven will not compare the size of different files with one version.