案例11 基于Maven构建实现学生新增案例

简介: 基于Maven快速构建,实现学生新增。

 基于Maven快速构建,实现学生新增。

1. 创建Maven项目

创建Maven项目,项目名称为case11-maven02。


2. 配置Maven依赖

编辑pom.xml

<?xmlversion="1.0" encoding="UTF-8"?><projectxmlns="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.wfit</groupId><artifactId>maven02</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!-- spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.25</version></dependency><!-- commons-loging --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><!-- junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies></project>


3. 更新Maven


4. 创建resources目录

    • src.main路径下,执行new – Directory操作。


      • 选择resources后,执行回车键。


        • src.main目录下创建了java和resource目录。


        5. 创建Spring配置文件

        src.main.resources目录下创建applicationContext.xml配置文件。

        <?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--开启组件扫描--><context:component-scanbase-package="com.wfit"/></beans>


        6. 创建StudentService接口

        src.main.java目录下创建com.wfit.service包,此包目录下创建StudentService接口,声明addStudent方法。

        publicinterfaceStudentService {
        //新增学生信息publicvoidaddStudent();
        }


        7. 创建StudentServiceImpl实现类

        src.main.java目录下创建com.wfit.service.impl包,此包目录下创建StudentServiceImpl实现类,实现addStudent方法。

        @Service//标注业务逻辑组件publicclassStudentServiceImplimplementsStudentService {
        @Autowired//@Autowired注解 完成自动配置privateStudentDaostudentDao;
        @OverridepublicvoidaddStudent() {
        //调用StudentDao中的saveStudent方法studentDao.saveStudent();
            }
        }


        8. 创建StudentDao类

        src.main.java目录下创建com.wfit.dao包,此包目录下创建StudentDao.java类。

        @Repository//标注数据访问层publicclassStudentDao {
        //保存学生信息publicvoidsaveStudent(){
        System.out.println("保存学生信息成功!");
            }
        }


        9. 创建测试类

        src.test.java.com.wfit目录下创建StudentTest测试类。

        publicclassStudentTest {
        @Testpublicvoidtest(){
        //初始化Spring容器ApplicationContext,加载配置文件ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器获取StudentServiceImpl实例StudentServicestudentService=                (StudentService) applicationContext.getBean("studentServiceImpl");
        studentService.addStudent();
            }
        }


        10. 执行结果



        目录
        相关文章
        |
        1月前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven构建配置文件可设置或覆盖默认构建值,支持按环境(如开发、测试、生产)定制构建。通过`pom.xml`中的`activeProfiles`或`profiles`元素定义,并可通过多种方式激活。配置文件分项目级(POM内)、用户级(`settings.xml`)和全局级(Maven配置目录下的`settings.xml`)。特定环境下,如指定构建配置文件为`prod`时,项目将使用对应的配置文件如`env.prod.properties`。
        |
        4天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件
        |
        6天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件
        |
        2天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件
        |
        10天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件
        |
        8天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件
        |
        4天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件
        |
        22天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件
        |
        14天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件
        |
        12天前
        |
        XML Java Maven
        Maven 构建配置文件
        Maven 构建配置文件