mysql2014 三合一_SpringMVC4 + MyBatis3 + SQL Server 2014整合教程(含增删改查分页)

前言

说起整合自然离不开ssm,我本身并不太喜欢ORM,尤其是MyBatis,把SQL语句写在xml里,尤其是大SQL,可读性不高,出错也不容易排查。

开发环境

idea2016、SpringMVC4、Mybatis3

项目结构

314f37350fc7ad886593140971e738d6.png

SSM整合

1、pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.autohome

SpringMVC3

war

1.0-SNAPSHOT

SpringMVC3

http://maven.apache.org

junit

junit

4.10

org.springframework

spring-core

4.3.6.RELEASE

org.springframework

spring-beans

4.3.6.RELEASE

org.springframework

spring-context

4.3.6.RELEASE

org.springframework

spring-web

4.3.6.RELEASE

org.springframework

spring-context-support

4.3.6.RELEASE

org.springframework

spring-webmvc

4.3.6.RELEASE

org.springframework

spring-jdbc

4.3.6.RELEASE

org.apache.velocity

velocity

1.6.2

org.apache.velocity

velocity-tools

2.0

org.mybatis

mybatis

3.4.2

org.mybatis

mybatis-spring

1.3.0

com.microsoft.sqlserver

sqljdbc4

4.0

commons-dbcp

commons-dbcp

1.4

SpringMVC3

2、web.xml

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

SpringMVC

/

3、applicationContext.xml无配置内容所以忽略

4、springmvc-servlet.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

">

5、springmvc-mybatis.xml

/p>

"http://mybatis.org/dtd/mybatis-3-config.dtd">

6、dao接口层、mapper(dao接口实现层)、Biz层、 model层忽略不计(id,name,address3个测试字段)。 mapper文件让我踩了坑,后恍然大悟,mapper.xml要放在resources包下。

public interface UserMapper {

List listAllUser();

List listPagedUser(@Param("pageIndex") int pageIndex,@Param("pageSize") int pageSize);

int count();

int updateUser(User user);

int deleteUser(int id);

int insertUser(User user);

User getUserById(int id);

}

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from t_userinfo

select top ${pageSize} * from t_userinfo where id not in (select top (${pageSize} * (${pageIndex} -1)) id from t_userinfo)

select count(*) from t_userinfo

insert into t_userinfo VALUES (#{name},#{address})

UPDATE t_userinfo set name=#{name},address=#{address} where id=#{id}

DELETE FROM t_userinfo where id=#{id}

select * from t_userinfo where id=#{id}

public interface IUserBiz {

List listAllUser();

List listPagedUser(@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

int count();

int updateUser(User user);

int deleteUser(int id);

int insertUser(User user);

User getUserById(int id);

}

package com.autohome.service;

import com.autohome.model.User;

import com.autohome.mapper.UserMapper;

import org.apache.ibatis.annotations.Param;

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

import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Map;

@Service

public class UserBizImpl implements IUserBiz {

@Autowired

private UserMapper userMapper;

public List listAllUser() {

return userMapper.listAllUser();

}

public List listPagedUser(@Param("pageIndex") int pageIndex,@Param("pageSize") int pageSize) {

return userMapper.listPagedUser(pageIndex,pageSize);

}

public int count() {

return userMapper.count();

}

public int updateUser(User user) {

return userMapper.updateUser(user);

}

public int deleteUser(int id) {

return userMapper.deleteUser(id);

}

public int insertUser(User user) {

return userMapper.insertUser(user);

}

public User getUserById(int id) {

return userMapper.getUserById(id);

}

}

7、Controller。 我新建了一个UserController,在这里调用了增删改查分页的操作

package com.autohome.controller;

import com.autohome.service.IUserBiz;

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

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.autohome.model.User;

@Controller

@RequestMapping("/User")

public class UserController {

@Autowired

private IUserBiz userBiz;

@RequestMapping("/index")

public ModelAndView index(){

//System.out.println("size:"+userBiz.listAllUser().size());

System.out.println("size:"+userBiz.count());

//

// User user =new User();

// user.setName("张三");

// user.setAddress("shanxi");

//

// int result = userBiz.insertUser(user);

// if(result>0)

// {

// System.out.println("insert success");

// }else{

// System.out.println("insert err");

// }

int result = userBiz.deleteUser(39);

if(result>0)

{

System.out.println("delete success");

}else{

System.out.println("delete err");

}

// User user =new User();

// user.setId(35);

// user.setName("张三11111");

// user.setAddress("china");

//

// int result = userBiz.updateUser(user);

// if(result>0)

// {

// System.out.println("update success");

// }else{

// System.out.println("update err");

// }

//System.out.println("size:"+userBiz.listPagedUser(1,10).size());

ModelAndView mav=new ModelAndView("index");

return mav;

}

}

总结

做这个demo前我看的ssm整合教程全部是基于myeclipse开发的,而且教程把dao接口和dao实现是全部放在src java目录下的,也就是mapper目录包括了mapper接口和mapper.xml。 我做第一个demo时在idea里也是这么做的,demo运行始终不成功,一直提示找不 到mapper.xml里的方法,后来编译的时候我发现target/classes里确实找不到mapper.xml。 不知道用myeclipse整合开发时是否遇到这个问题,后我把mapper.xml文件放到resources目录中,编译后target文件总就能找到mapper.xml。 方法运行也搞定了。写demo写了半个小时,debug这个问题花了2个小时,好在demo跑起来了,也算是有收获的。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值