SpringMVC4 + MyBatis3 + SQL Server 2014整合教程(含增删改查分页)

网友投稿 246 2023-05-10


SpringMVC4 + MyBatis3 + SQL Server 2014整合教程(含增删改查分页)

前言

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

开发环境

idea2016、SpringMVC4、Mybatis3

项目结构

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

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

HhdLaJzyEv

"-//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://w3.org/2001/XMLSchema-instance"

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

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

">

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

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

">

5、springmvc-mybatis.xml

"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);

}

"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跑起来了,也算是有收获的。

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue脚手架vue
下一篇:Java并发编程之Semaphore(信号量)详解及实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~