SSM整合【part1】搭建Spring环境

SSM整合【part1】搭建Spring环境,第1张

SSM整合【part1】搭建Spring环境 SSM整合 part1 1. 搭建整合环境

1.1 首先创建一个maven项目,使用archetypeCatalog=internal可以解决maven项目创建过慢的问题。

接下来在pom.xml中导入相关jar包的坐标。


    UTF-8
    1.8
    1.8
    5.0.2.RELEASE
    1.6.6
    1.2.12
    8.0.11
    3.4.5



    
    
      org.aspectj
      aspectjweaver
      1.6.8
    

    
      org.springframework
      spring-aop
      ${spring.version}
    

    
      org.springframework
      spring-context
      ${spring.version}
    

    
      org.springframework
      spring-web
      ${spring.version}
    

    
      org.springframework
      spring-webmvc
      ${spring.version}
    

    
      org.springframework
      spring-test
      ${spring.version}
    

    
      org.springframework
      spring-tx
      ${spring.version}
    

    
      org.springframework
      spring-jdbc
      ${spring.version}
    

    
      junit
      junit
      4.12
      compile
    

    
      mysql
      mysql-connector-java
      ${mysql.version}
    

    
      javax.servlet
      servlet-api
      2.5
      provided
    

    
      javax.servlet.jsp
      jsp-api
      2.0
      provided
    

    
      jstl
      jstl
      1.2
    

    
    
      log4j
      log4j
      ${log4j.version}
    

    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    

    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    
    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    

    
      org.mybatis
      mybatis-spring
      1.3.0
    

    
      c3p0
      c3p0
      0.9.1.2
      jar
      compile
    

1.2 在resources创建log4f.properties。

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, ConSOLE            debug   info   warn error fatal
log4j.rootCategory=info, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, ConSOLE

# ConSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.ConSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %mn

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %mn

1.3 设计好数据库和编写domain包。

mysql:
    id int primary key auto_increment,
    name varchar(20),
    money double 
    
domain:
    private Integer id;
    private String name;
    private Double money;

1.4 初步架构好持久层和业务层。

public void saveAccount(Account account); 
public List findAll();
2. 搭建和测试Spring框架的开发环境

2.1 创建applicationContext.xml的配置文件,导入spring框架的xml-dtd约束。



    

2.2 开启注解扫描,只扫描service层和dao层的注解,所以要忽略web层注解,因为web层准备让SpringMVC框架去管理。


	

2.3 为web层、service层和dao层添加注入注解,并在service层中添加dao层bean对象的自动注入注解。

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
	//略
}
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    
    @Autowired
    private AccountDao accountDao;
    
	//略
}
@Controller("accountController")
public class AccountController {
	//略
}
3. 单元测试
	@Test
    public void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        AccountService as = (AccountService) ac.getBean("accountService");
        as.findAll();
    }

以上,Spring环境就搭建完成,之后我们先开始Spring整合SpringMVC。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5712237.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存