1。首先需用ECLIPSE创建MAVEN工程,这里有几种方式,每一种都或多或少需要一些改动,比如少src/main/java,再比如初始为j2se1.5,亦或比如WEB-INF/web.xml缺失(可能以后都为非默认项了)等。初学者为简化搭建步骤(主要是少出错),可参考先建WEB Dynamic Web Project 项目,然后转configure ->convert to Maven Project;或NEW->MAVEN PROJECT->NEXT->Maven Webapp project->NEXT生成,这样改动的地方也比较少。
2。建好MAVEN后将各资源分别放置各包中(可根据自身的项目需要重命名),比如XX.DAO,XX.SERVICE,XX.POJO,XX.ENTITY,XX.VO,XX.YY什么的,源码地址:
https://github.com/pregalaxy/springmvc.git
3。POM.XML
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
已改成目前最新版,MYBATIS生成文件略(已改成全注解形式,原XML意义不大,如XML配置方式本站另有专门文档介绍。)
4。WEB.XML改成如下方式启动:
package com.bank.web;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MyWebApplicationInitializer implements WebApplicationInitializer {
// @Override
public void onStartup(ServletContext servletCxt) {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(AppConfig.class);
ac.refresh();
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(ac);
ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}
5。相关配置文件:
package com.bank.web;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.bank.controller,com.bank.service.impl")
@MapperScan("com.bank.dao")
public class AppConfig {
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&character=utf8");
dataSource.setUsername("root");
dataSource.setPassword("");
return dataSource;
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory.getObject();
}
}
6.Controller:
package com.bank.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bank.service.BankService;
@Controller
public class AccountController {
@Autowired
private BankService bankService;
public BankService getBankService() {
return bankService;
}
public void setBankService(BankService bankService) {
this.bankService = bankService;
}
@RequestMapping("/open")
public String openAccount() {
System.out.println("ok opened!");
//各种测试,可直接改名即用
// BankServiceImpl bankServiceImpl=new BankServiceImpl();
// BankDAOImplDB bankDAOImplDB=new BankDAOImplDB();
// bankServiceImpl.setBankDAO(bankDAOImplDB);
// bankServiceImpl.openAccount();
bankService.openAccount();
System.out.println("1");
return "index.jsp";
}
}
7.DAO
package com.bank.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.bank.pojo.Temp;
//这里的叫法也有多种,比如MAPPER什么的,不过这里面都是@INSERT,@DELETE,@UPDATE之类的打上标记写SQL的地方
@Mapper
public interface TempDAO {
@Select("select * from temp where aa = #{id}")
public Temp selectTemp(int id);
@Select("select * from temp")
public List
8.ServiceImpl类加注解
package com.bank.service.impl;
import java.util.Date;
import java.util.Scanner;
import java.util.UUID;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.bank.dao.TempDAO;
import com.bank.pojo.Temp;
import com.bank.pojo.Users;
import com.bank.service.BankService;
@Service
public class BankServiceImpl implements BankService{
下面的各种测试注解都不用加,这里只有@service,与@Autowired注入哪个DAO即可
// @MyDAO(name="BankDAOImplMap")
// @Autowired
// @Qualifier("bankDAOImplDB")
// private BankDAO bankDAO;
@Autowired
private TempDAO tempDAO;
// public BankDAO getBankDAO() {
// return bankDAO;
// }
//
// public void setBankDAO(BankDAO bankDAO) {
// this.bankDAO = bankDAO;
// }
public Users openAccount() {
System.out.println("sercie");
System.out.println(tempDAO.selectTemp(1).getBb());
return null;
}
}
9.service、实体类、数据库建表、HTML什么的略,到此框架基本搭建完成,对一般应用扩展新建应用类打对标签,照格式写即可