#spring+springMVP+mybatis框架搭建(下)

接上回
SqlMapConfig.xml配置

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

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 test

3.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
借鉴自:梁城月

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值