可同时参考:https://blog.csdn.net/q283614346/article/details/80904945
4.0.0
com.yingjun.test
TradingState
war
2.0.1
UTF-8
yyyyMMddHHmmss
3.2.9.RELEASE
3.1.1
1.1.1
org.springframework
spring-core
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatisspring.version}
mysql
mysql-connector-java
5.1.34
junit
junit
4.11
test
c3p0
c3p0
0.9.1.2
org.aspectj
aspectjweaver
1.8.1
javax.servlet
jstl
1.2
javax.servlet
servlet-api
3.0
provided
javax.servlet.jsp
jsp-api
2.2
provided
commons-fileupload
commons-fileupload
1.3.1
commons-lang
commons-lang
2.6
commons-codec
commons-codec
1.9
org.apache.httpcomponents
httpclient
4.5
org.slf4j
slf4j-api
1.7.10
org.slf4j
slf4j-log4j12
1.7.10
log4j
log4j
1.2.17
com.alibaba
fastjson
1.1.41
org.codehaus.jackson
jackson-mapper-asl
1.9.13
maven-compiler-plugin
2.3.2
1.7
1.7
maven-war-plugin
2.2
3.0
false
${project.artifactId}_${project.version}_${maven.build.timestamp}
contextConfigLocation
classpath:spring.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springMVC
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
四、spring-mybatis
为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现:MapperFactoryBean。这个类 可以让你直接注入数据映射器接口到你的 service 层 bean 中。当使用映射器时,你仅仅如调 用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring 将会为你创建代理。
spring-mybatis.xml
五、spring-mvc.xml
text/html;charset=UTF-8
六、jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.194:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
c3p0.pool.size.max=20
c3p0.pool.size.min=5
c3p0.pool.size.ini=3
c3p0.pool.size.increment=2
七、log4j.properties
log4j.rootLogger=info, console, debug, app, error
###Console ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d %p[%C:%L]- %m%n
### debug ###
log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File = log/debug.log
log4j.appender.debug.Append = true
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.DatePattern='.'yyyy-MM-dd
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern = %d %p[%c:%L] - %m%n
### app ###
log4j.appender.app = org.apache.log4j.DailyRollingFileAppender
log4j.appender.app.File = log/app.log
log4j.appender.app.Append = true
log4j.appender.app.Threshold = INFO
log4j.appender.app.DatePattern='.'yyyy-MM-dd
log4j.appender.app.layout = org.apache.log4j.PatternLayout
log4j.appender.app.layout.ConversionPattern = %d %p[%c:%L] - %m%n
### Error ###
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = log/error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.DatePattern='.'yyyy-MM-dd
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern =%d %p[%c:%L] - %m%n
八、generatorl.xml
利用MyBatis Generator自动创建实体类、映射文件以及DAO接口
打开CMD窗口 进入该目录结构,输入命令行:
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
九、建立Service层
package com.yingjun.test.service;
public interface OtherServiceI {
public String getOterList();
}
package com.yingjun.test.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yingjun.test.dao.OtherListMapper;
import com.yingjun.test.model.OtherList;
import com.yingjun.test.model.OtherListDomain;
@Service
public class OtherServiceImpl implements OtherServiceI {
@Autowired
private OtherListMapper otherListMapper;
@Override
public String getOterList() {
Set set=new HashSet();
List list=otherListMapper.selectAll();
List jsonList=new ArrayList();
for(OtherList other:list){
String title=other.getTitle();
if(set.contains(title)){
continue;
}else{
List t_list=new ArrayList();
for(OtherList data:list){
if(title.equals(data.getTitle())){
t_list.add(data);
}
}
OtherListDomain domain=new OtherListDomain();
domain.setTitle(title);
domain.setItems(t_list);
jsonList.add(domain);
set.add(other.getTitle());
}
}
return JSON.toJSONString(jsonList, SerializerFeature.WriteMapNullValue);
}
}
十、建立conrorller层
package com.yingjun.test.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.alibaba.fastjson.JSON;
import com.yingjun.test.service.OtherServiceI;
import com.yingjun.test.service.StockStatusServiceI;
@Controller
@RequestMapping(value = "/")
public class TSSController {
@Autowired
private OtherServiceI otherService;
@RequestMapping(value="/getOtherList",produces="text/html;charset=UTF-8" )
@ResponseBody
private String getOtherList(){
String json=otherService.getOterList();
return json;
}
}
十一、建立测试类
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
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.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class TestMybatis {
@Autowired
private OtherServiceI service;
@Test
public void test() {
String list=service.getOterList();
logger.info(list);
}
}
十二、通过浏览器进行验证
http://localhost:8080/xxx/getOtherList