您的位置:首页 > 其它

2017/09/03简单搭建SSM框架

2017-09-03 11:22 351 查看

简单搭建SSM框架

1.由于熟悉了使用Maven,导包太麻烦了,所以还是使用Maven+Spring+Spring MVC+Mybatis的方式简单搭建一个SSM框架。

2.数据库使用的是Mysql,想要使用Oracle或者其它数据库的,可以在POM.xml中修改相关的依赖包,还有修改jdbc.properties的相关配置即可。

3.现在开始简单介绍一下搭建的相关过程。

a.安装maven插件(依赖包的路径使用的是阿里云的maven数据源

b.新建一个maven project  ----->  直接next  ------>选择Artifact
id中尾缀为webapp的一项 ------->输入group id和Artifact id----->点击完成

c.先利用maven导入相关的依赖包:

4.0.0combuildSSMwar0.0.1-SNAPSHOTbuildSSM Maven Webapphttp://maven.apache.org4.0.5.RELEASE6.8.8junitjunit3.8.1testjavax.servletjavax.servlet-api3.1.0javax.servlet.jspjsp-api2.2javax.servletjstl1.2org.springframeworkspring-jdbc4.0.0.RELEASEorg.springframeworkspring-expressionorg.springframeworkspring-contextorg.springframeworkspring-web4.0.0.RELEASEorg.springframeworkspring-webmvc4.0.0.RELEASEorg.springframeworkspring-test4.0.0.RELEASEorg.mybatismybatis3.2.8org.mybatismybatis-spring1.2.1commons-fileuploadcommons-fileupload1.3.1commons-langcommons-lang2.6commons-codeccommons-codec1.9org.apache.commonscommons-pool22.4.2org.apache.commonscommons-dbcp22.1.1log4jlog4j1.2.17cglibcglib-nodep2.2.2mysqlmysql-connector-java5.1.25taglibsstandard1.1.2net.sf.json-libjson-lib2.4jdk15com.fasterxml.jackson.corejackson-core2.7.4com.fasterxml.jackson.corejackson-annotations2.7.4com.fasterxml.jackson.corejackson-databind2.7.4com.lnsforg.apache.maven.pluginsmaven-compiler-plugin3.31.71.7org.springframeworkspring-expression4.0.0.RELEASEorg.springframeworkspring-context4.0.0.RELEASE


d.新建实体类包(Entity),新建实体类

e.新建service层,定义各种方法接口

f.新建dao层,定义数据操作类,可以用xml或者直接注解写SQl语句

g.实现serviceImpl层

h.新建test包

I.添加spring mvc的配置文件

<?xml version="1.0" encoding="UTF-8" ?>

j.添加bean.xml

<?xml version="1.0" encoding="UTF-8"?>


k.添加数据库配置和错误提示配置

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/buildssm
username=root
password=root

log4j.rootLogger=DEBUG,Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=INFO

l.修改项目的web.xml

<?xml version="1.0" encoding="UTF-8"?>Archetype Created Web ApplicationcontextConfigLocationclasspath:beans.xmlorg.springframework.web.context.ContextLoaderListener
index.htmlindex.htmindex.jspdefault.htmldefault.htmdefault.jspdispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocationclasspath:springmvc.xml1dispatcherServlet/HiddenHttpMethodFilterorg.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter/*springUtf8Encodingorg.springframework.web.filter.CharacterEncodingFilter
encodingUTF-8forceEncodingtrue


4.项目整体目录结构如下:

5.controller的代码示例:

package com.lnsf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lnsf.entity.TestEntity;
import com.lnsf.service.TestService;
import com.lnsf.service.Impl.TestServiceImpl;

@Controller
public class TestController {
@Autowired
TestService testService;

@RequestMapping("selectAllTest")
@ResponseBody
public TestEntity selectAllTest(){
TestEntity test= testService.selectAllTest();
return test;
}

}


6.dao层的xml写法和注解写法

<?xml version="1.0" encoding="UTF-8" ?>


package com.lnsf.dao;

import org.apache.ibatis.annotations.Select;

import com.lnsf.entity.TestEntity;

public interface TestDao {
public TestEntity selectAllTest();

@Select("select * from test")
public TestEntity selectAllTest2();

@Select("select * from test")
public TestEntity selectAllTest3();
}


7.service层的示例

package com.lnsf.service;

import com.lnsf.entity.TestEntity;

public interface TestService {
public TestEntity selectAllTest();
public TestEntity selectAllTest2();
public TestEntity selectAllTest3();
}


8.serviceImpl层的示例

package com.lnsf.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lnsf.dao.TestDao;
import com.lnsf.entity.TestEntity;
import com.lnsf.service.TestService;
@Service("testService")
public class TestServiceImpl implements TestService {
@Autowired
TestDao testDao;

@Override
public TestEntity selectAllTest() {
// TODO Auto-generated method stub
return testDao.selectAllTest();
}

@Override
public TestEntity selectAllTest2() {
// TODO Auto-generated method stub
return testDao.selectAllTest2();
}

@Override
public TestEntity selectAllTest3() {
// TODO Auto-generated method stub
return testDao.selectAllTest3();
}
}


9.test层的示例
package com.lnsf.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.lnsf.dao.TestDao;
import com.lnsf.entity.TestEntity;
@RunWith(SpringJUnit4ClassRunner.class) //使用Springtest测试框架
@ContextConfiguration("/beans.xml") //加载配置
public class testSSm {
@Autowired
TestDao testDao;

@Test
public void tt(){
TestEntity test=new TestEntity();
test=testDao.selectAllTest2();
System.out.println(test);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: