maven作用原理及父子级继承使用
maven主要作用:在需要高并发访问条件下构建分区集群项目时,提供一个稳定版本依赖和排除重复jar包引入,减少结构冗余,从侧面增加代码运行速度
首先,我们要测试父子级继承依赖,就需要准备项目,目录如下:
首先在父工程(test)的pom.xml文件中引入经过dependencyManagement束缚的jar包,以便子工程进行重写引用
<?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>com.aaa.fx.maven</groupId> <artifactId>test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <!--//此处为已经被引用的module文件:--> <modules> <module>mapper</module> <module>model</module> <module>service</module> <module>web</module> </modules> <!--dependencyManagement此标签为设定jar包不会对子对象继承,子对象若需要继承则需要在自己的pom.xml文件中进行特殊声明重写,需要注意的是。重写的时候不需要加版本号--> <dependencyManagement> <dependencies> <!-- 此为mysql的驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.21</version> </dependency> <!-- spring --> <!-- mybatis --> <!-- shiro --> </dependencies> </dependencyManagement> </project>
接着在model中书写实体类:
package com.aaa.fx.maven.model; import java.io.Serializable; import java.util.Objects; /** * 首先,实体类需要实现序列化接口 * 定义对象属性全部使用引用类型,防止空指针异常(引用类型为空则为null) * 实现getset方法
* 重写equals和hashCode方法 */ public class User implements Serializable { private Long id; private String username; private String password; private Integer age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return Objects.equals(id, user.id) && Objects.equals(username, user.username) && Objects.equals(password, user.password) && Objects.equals(age, user.age); } @Override public int hashCode() { return Objects.hash(id, username, password, age); } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + '}'; } }
由于实体类无需特殊引用 所以接下来直接进到mapper目录下书写mapper的pom.xml文件:
<?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"> <parent> <artifactId>test</artifactId> <groupId>com.aaa.fx.maven</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>mapper</artifactId> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!--无需书写版本号,因为此为继承/重写敷方法中的dependencyManagement中的jar包--> <!--若强行书写,则会到仓库中下载,造成jar包导入重复和冗余--> </dependency> <!--需要用到实体类,则在此处引入model实体类,便可以在mapper的Module下使用model中的实体类,此为跨域引用--> <dependency> <groupId>com.aaa.fx.maven</groupId> <artifactId>model</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
接下来我肯可以看到 已经可以成功使用我们在model中定义好的User对象了
package com.aaa.fx.maven.mapper; import com.aaa.fx.maven.model.User; import java.util.List; public interface UserMapper { List<User> selectAll(); }
进入service中配置servie的pom.xml文件:
<?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"> <parent> <artifactId>test</artifactId> <groupId>com.aaa.fx.maven</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service</artifactId> <!--service需要用到mapper,故在此处引入mapper,进行跨域--> <dependencies> <dependency> <groupId>com.aaa.fx.maven</groupId> <artifactId>mapper</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
书写我们的service:
package com.aaa.fx.maven.service; import com.aaa.fx.maven.mapper.UserMapper; import com.aaa.fx.maven.model.User; import java.util.ArrayList; import java.util.List; public class UserService { UserMapper userMapper = new UserMapper() { public List<User> selectAll() { // 搞一个User对象放入List<User>的集合当中映射数据库返回值对象 User user = new User(); //1L的L有必要大写,和小写的I和1用以区分 user.setId(1L); user.setAge(15); user.setUsername("admin"); user.setPassword("Moon-history"); // 这不,搞出来一个List<User>类型的arraylist对象,然后将设置好值的user放进去 List<User> userList = new ArrayList<User>(); userList.add(user); return userList; } }; }
可以看到我们成功引用了mapper
最后的web中的pom.xml:
<?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"> <parent> <artifactId>test</artifactId> <groupId>com.aaa.fx.maven</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>web</artifactId> <!--当然,最后需要在页面显示测试了,那么引入service文件--> <dependencies> <dependency> <groupId>com.aaa.fx.maven</groupId> <artifactId>service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
接着是最后的web中的controller:
package com.aaa.fx.maven.web; import com.aaa.fx.maven.service.UserService; public class UserController { UserService userService = new UserService(); //成功搞到,那么结束! }
The end~~~~~~