JavaEE framework integration development entry to actual combat: Spring+SpringMVC+MyBAtis (micro-class version)-the first chapter of code exercises

Pure hand knock, thank you for your support

	使用eclipse编写,工程目录结构如下:

Insert picture description here

The first unit

Directory structure
Insert picture description here
The Spring core jar used is placed in the lib directory of WEB-INF

TestDao.java

package dao;

public interface TestDao {
    
    
	public void sayHello();
}

TestDaoImpl.java

package dao;

public class TestDaoImpl implements TestDao {
    
    

	@Override
	public void sayHello() {
    
    
		// TODO Auto-generated method stub
        System.out.println("Hello,Study hard!");
	}

}

Test.java

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

import dao.TestDao;

public class test {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		/*初始化Spring容器,加载配置文件
		   BeanFactory工厂
		  BeanFactory beanFac=new XmlBeanFactory(new FileSystemResource("C:\\Users\\gao\\Desktop\\Spring环境搭建\\ch1\\src\\applicationContext.xml"));      
		   TestDao tt=(TestDao)beanFac.getBean("test");
		 */
			 
		//ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
        //TestDao tt=(TestDao)appCon.getBean("test");
		ApplicationContext appcon=
				new FileSystemXmlApplicationContext("C:\\\\Users\\\\gao\\\\Desktop\\\\Spring环境搭建\\\\ch1\\\\src\\\\applicationContext.xml");
		TestDao tt=(TestDao)appcon.getBean("test");
		tt.sayHello();
	}
}

application.xml

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

import dao.TestDao;

public class test {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		/*初始化Spring容器,加载配置文件
		   BeanFactory工厂
		  BeanFactory beanFac=new XmlBeanFactory(new FileSystemResource("C:\\Users\\gao\\Desktop\\Spring环境搭建\\ch1\\src\\applicationContext.xml"));      
		   TestDao tt=(TestDao)beanFac.getBean("test");
		 */
			 
		//ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
        //TestDao tt=(TestDao)appCon.getBean("test");
		ApplicationContext appcon=
				new FileSystemXmlApplicationContext("C:\\\\Users\\\\gao\\\\Desktop\\\\Spring环境搭建\\\\ch1\\\\src\\\\applicationContext.xml");
		TestDao tt=(TestDao)appcon.getBean("test");
		tt.sayHello();
	}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ch1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41827511/article/details/115030826