接上回
SqlMapConfig.xml配置
2.4、log4j.properties配置
log4j.rootLogger = debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p:%-d{yyyy-MM-dd HH:mm:ss} %m%n
三、代码实现
3.1、代码结构
3.2、pojo
3.2.1、Test.java
package com.lcy.pojo;
public class Test {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.3、mapper
3.3.1、TestMapper.java
package com.lcy.mapper;
import java.util.List;
import com.lcy.pojo.Test;
public interface TestMapper {
public List findTests();
}
3.3.2、TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?> select * from test3.4、service
3.4.1、TestService.java
package com.lcy.service;
import java.util.List;
import com.lcy.pojo.Test;
public interface TestService {
public List findTests();
}
3.5、service.impl
3.5.1、TestServiceImpl.java
package com.lcy.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import com.lcy.mapper.TestMapper;
import com.lcy.pojo.Test;
import com.lcy.service.TestService;
@Transactional
public class TestServiceImpl implements TestService {
@Autowired
@Qualifier(“testMapper”)
private TestMapper testMapper;
@Override
public List<Test> findTests() {
return testMapper.findTests();
}
}
3.6、controoller
3.6.1、TestController.java
package com.lcy.comtroller;
import java.util.List;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lcy.pojo.Test;
import com.lcy.service.TestService;
@Controller
public class TestController {
@Autowired
@Qualifier(“testService”)
private TestService testService;
@RequestMapping("/TestToJson")
@ResponseBody
public String TestToJson() {
List tests=testService.findTests();
JSONArray jsonArray=JSONArray.fromObject(tests);
return jsonArray.toString();
}
@RequestMapping("/Hello")
@ResponseBody
public String Helle() {
return “Hello SSM”;
}
}
3.7、test
3.7.1、TestServiceTest.java
package com.lcy.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lcy.service.TestService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations=“classpath:com/lcy/config/applicationContext.xml”)
public class TestServiceTest {
@Autowired
@Qualifier(“testService”)
private TestService testService;
@Test
public void testFindTests() {
System.out.println(testService.findTests());
}
}
参考网址https://www.cnblogs.com/yscj-lcy/p/7586102.html
借鉴自:梁城月