欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-12 22:07:46
前言 说起整合自然离不开ssm,我本身并不太喜欢orm,尤其是mybatis,把sql语句写在xml里,尤其是大sql,可读性不高,出错也不容易排查。 开发环境 id...

前言

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

开发环境

idea2016、springmvc4、mybatis3

项目结构

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

ssm整合

1、pom.xml

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>com.autohome</groupid>
 <artifactid>springmvc3</artifactid>
 <packaging>war</packaging>
 <version>1.0-snapshot</version>
 <name>springmvc3</name>
 <url>http://maven.apache.org</url>
 <dependencies>
 <dependency>
  <groupid>junit</groupid>
  <artifactid>junit</artifactid>
  <version>4.10</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-core</artifactid>
  <version>4.3.6.release</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-beans</artifactid>
  <version>4.3.6.release</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-context</artifactid>
  <version>4.3.6.release</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-web</artifactid>
  <version>4.3.6.release</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-context-support</artifactid>
  <version>4.3.6.release</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-webmvc</artifactid>
  <version>4.3.6.release</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-jdbc</artifactid>
  <version>4.3.6.release</version>
 </dependency>
 <dependency>
  <groupid>org.apache.velocity</groupid>
  <artifactid>velocity</artifactid>
  <version>1.6.2</version>
 </dependency>
 <dependency>
  <groupid>org.apache.velocity</groupid>
  <artifactid>velocity-tools</artifactid>
  <version>2.0</version>
 </dependency>
 <dependency>
  <groupid>org.mybatis</groupid>
  <artifactid>mybatis</artifactid>
  <version>3.4.2</version>
 </dependency>
 <dependency>
  <groupid>org.mybatis</groupid>
  <artifactid>mybatis-spring</artifactid>
  <version>1.3.0</version>
 </dependency>
 <dependency>
  <groupid>com.microsoft.sqlserver</groupid>
  <artifactid>sqljdbc4</artifactid>
  <version>4.0</version>
 </dependency>
 <dependency>
  <groupid>commons-dbcp</groupid>
  <artifactid>commons-dbcp</artifactid>
  <version>1.4</version>
 </dependency>
 </dependencies>
 <build>
 <finalname>springmvc3</finalname>
 </build>
</project>

2、web.xml

<!doctype web-app public
 "-//sun microsystems, inc.//dtd web application 2.3//en"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>archetype created web application</display-name>
 <!--告知javaee容器,有那些内容需要添加到上下文里去-->
 <context-param>
 <param-name>contextconfiglocation</param-name>
 <param-value>classpath:applicationcontext.xml</param-value>
 </context-param>
 <listener>
 <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
 </listener>
 <!--spring 前端控制器-->
 <servlet>
 <servlet-name>springmvc</servlet-name>
 <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
 <init-param>
  <param-name>contextconfiglocation</param-name>
  <param-value>classpath:springmvc-servlet.xml</param-value>
 </init-param>
 </servlet>
 <servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

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

4、springmvc-servlet.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  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
">

 <!--从配置文件加载数据库信息-->
 <bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">
  <property name="locations" value="classpath:config/jdbc.properties"/>
  <property name="fileencoding" value="utf-8"/>
 </bean>

 <!--配置数据源,这里使用spring默认-->
 <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource">
  <property name="driverclassname" value="${sqlserver.driver}"/>
  <property name="url" value="${sqlserver.url}"/>
  <property name="username" value="${sqlserver.username}"/>
  <property name="password" value="${sqlserver.password}"/>
 </bean>

 <!--扫描mapper-->
 <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
  <property name="basepackage" value="com.autohome.mapper"/>

 </bean>

 <!--配置sqlsessionfactory-->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="configlocation" value="classpath:springmvc-mybatis.xml"/>
  <property name="datasource" ref="datasource"/>
 </bean>

 <!--启用最新的注解器、映射器-->
 <mvc:annotation-driven/>

 <!--扫描controller注解类-->
 <context:component-scan base-package="com.autohome.controller" />
 <!--扫描service注解类-->
 <context:component-scan base-package="com.autohome.service"/>

 <!--配置视图解析器-->
 <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
  <property name="prefix" value="/web-inf/views/"/>
  <property name="suffix" value=".jsp"/>
 </bean>

</beans>

5、springmvc-mybatis.xml

<?xml version="1.0" encoding="utf-8"?>
<!doctype configuration public "-//mybatis.org//dtd config 3.0//en"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

 <!-- 实体类,简称 -设置别名 -->
 <typealiases>
  <typealias alias="user" type="com.autohome.model.user" />
 </typealiases>


 <mappers>
  <mapper resource="mapper/usermapper.xml" />
 </mappers>

</configuration>

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

public interface usermapper {

 list<user> listalluser();

 list<user> 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);

 

} 
<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--命名空间和接口保持一致-->
<mapper namespace="com.autohome.mapper.usermapper">
 <select id="listalluser" resulttype="user">
  select * from t_userinfo
 </select>

 <select id="listpageduser" resulttype="user">
  select top ${pagesize} * from t_userinfo where id not in (select top (${pagesize} * (${pageindex} -1)) id from t_userinfo)
 </select>

 <select id="count" resulttype="int">
  select count(*) from t_userinfo
 </select>

 <insert id="insertuser" parametertype="user">
  insert into t_userinfo values (#{name},#{address})
 </insert>

 <update id="updateuser" parametertype="user">
  update t_userinfo set name=#{name},address=#{address} where id=#{id}
 </update>

 <delete id="deleteuser" parametertype="int">
  delete from t_userinfo where id=#{id}
 </delete>

 <select id="getuserbyid" resulttype="user" parametertype="int">
  select * from t_userinfo where id=#{id}
 </select>

</mapper>
public interface iuserbiz {

 list<user> listalluser();

 list<user> 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<user> listalluser() {

  return usermapper.listalluser();

 }

 

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

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

上一篇: Java泛型的简单实例

下一篇: