最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年06月06日 10:02:31   作者:garfieldzf  
這篇文章主要給大家介紹了關(guān)于SpringMVC4 + MyBatis3 + SQL Server 2014整合的相關(guān)資料,文中包括介紹了增刪改查分頁等相關(guān)內(nèi)容,通過示例代碼介紹的非常詳細(xì),分享出來供大家參考學(xué)習(xí),下面來一起看看吧。

前言

說起整合自然離不開ssm,我本身并不太喜歡ORM,尤其是MyBatis,把SQL語句寫在xml里,尤其是大SQL,可讀性不高,出錯也不容易排查。

開發(fā)環(huán)境

idea2016、SpringMVC4、Mybatis3

項(xiàng)目結(jié)構(gòu)

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容器,有那些內(nèi)容需要添加到上下文里去-->
 <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無配置內(nèi)容所以忽略

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

 <!--從配置文件加載數(shù)據(jù)庫信息-->
 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:config/jdbc.properties"/>
  <property name="fileEncoding" value="UTF-8"/>
 </bean>

 <!--配置數(shù)據(jù)源,這里使用Spring默認(rèn)-->
 <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>

 <!-- 實(shí)體類,簡稱 -設(shè)置別名 -->
 <typeAliases>
  <typeAlias alias="User" type="com.autohome.model.User" />
 </typeAliases>


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

</configuration>

6、dao接口層、mapper(dao接口實(shí)現(xiàn)層)、Biz層、 model層忽略不計(jì)(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,在這里調(diào)用了增刪改查分頁的操作

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;

 }

}

總結(jié)

做這個demo前我看的ssm整合教程全部是基于myeclipse開發(fā)的,而且教程把dao接口和dao實(shí)現(xiàn)是全部放在src java目錄下的,也就是mapper目錄包括了mapper接口和mapper.xml。 我做第一個demo時在idea里也是這么做的,demo運(yùn)行始終不成功,一直提示找不 到mapper.xml里的方法,后來編譯的時候我發(fā)現(xiàn)target/classes里確實(shí)找不到mapper.xml。 不知道用myeclipse整合開發(fā)時是否遇到這個問題,后我把mapper.xml文件放到resources目錄中,編譯后target文件總就能找到mapper.xml。 方法運(yùn)行也搞定了。寫demo寫了半個小時,debug這個問題花了2個小時,好在demo跑起來了,也算是有收獲的。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Java實(shí)現(xiàn)商品管理系統(tǒng)代碼實(shí)例講解

    Java實(shí)現(xiàn)商品管理系統(tǒng)代碼實(shí)例講解

    這篇文章主要介紹了Java實(shí)現(xiàn)商品管理系統(tǒng)代碼實(shí)例講解,文中代碼實(shí)例講解的很清楚,有需要的同學(xué)可以借鑒參考下
    2021-02-02
  • MyBatisCodeHelperPro最新激活方法(有效方法)

    MyBatisCodeHelperPro最新激活方法(有效方法)

    這篇文章主要介紹了MyBatisCodeHelperPro最新激活方法親測有效,非常好用,小編今天以idea2021.2.1為例給大家詳細(xì)講解,需要的朋友可以參考下
    2022-08-08
  • Spring boot 無法注入service問題

    Spring boot 無法注入service問題

    這篇文章主要介紹了Spring boot 無法注入service問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 詳解Spring AOP 攔截器的基本實(shí)現(xiàn)

    詳解Spring AOP 攔截器的基本實(shí)現(xiàn)

    本篇文章主要介紹了詳解Spring AOP 攔截器的基本實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Spring AI提示詞的四種使用方法分享

    Spring AI提示詞的四種使用方法分享

    提示詞是輸入給大模型(LLM)的文本指令,用于明確地告訴大模型你想要解決的問題或完成的任務(wù),也是大語言模型理解用戶需求并生成準(zhǔn)確答案的基礎(chǔ),那問題來了,在Spring AI如何用好提示詞?以及提示詞的使用方式有哪些呢?接下來本文一起來盤點(diǎn)一下
    2025-06-06
  • java并發(fā)編程專題(二)----如何創(chuàng)建并運(yùn)行java線程

    java并發(fā)編程專題(二)----如何創(chuàng)建并運(yùn)行java線程

    這篇文章主要介紹了java并發(fā)編程如何創(chuàng)建并運(yùn)行java線程,文中講解非常詳細(xì),示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 麒麟OS?(ARM64)?安裝MySQL、Redis、JDK、Docker超全指南

    麒麟OS?(ARM64)?安裝MySQL、Redis、JDK、Docker超全指南

    麒麟ARM操作系統(tǒng)是國企和政務(wù)機(jī)關(guān)推行信創(chuàng)化選擇率比較高的一款操作系統(tǒng),然而ARM操作系統(tǒng)非主流的X86系統(tǒng),除了命令一樣,在架構(gòu)方面差別極大,這篇文章主要介紹了麒麟OS?(ARM64)?安裝MySQL、Redis、JDK、Docker的相關(guān)資料,需要的朋友可以參考下
    2025-09-09
  • Spring?Boot項(xiàng)目通過RestTemplate調(diào)用三方接口的最佳實(shí)踐

    Spring?Boot項(xiàng)目通過RestTemplate調(diào)用三方接口的最佳實(shí)踐

    本文詳細(xì)講解了SpringBoot項(xiàng)目中使用RestTemplate調(diào)用第三方接口的實(shí)現(xiàn)方法,本教程將逐步指導(dǎo)您完成整個過程,確保結(jié)構(gòu)清晰、易于理解,感興趣的朋友跟隨小編一起看看吧
    2025-08-08
  • SpringBoot使用JavaCV處理rtsp流的示例代碼

    SpringBoot使用JavaCV處理rtsp流的示例代碼

    這篇文章主要為大家詳細(xì)介紹了SpringBoot使用JavaCV處理rtsp流,文中的示例代碼講解詳細(xì),具有一定的參考價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2024-02-02
  • Java二叉樹的遍歷思想及核心代碼實(shí)現(xiàn)

    Java二叉樹的遍歷思想及核心代碼實(shí)現(xiàn)

    今天小編就為大家分享一篇關(guān)于Java二叉樹的遍歷思想及核心代碼實(shí)現(xiàn),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01

最新評論

金塔县| 板桥市| 海阳市| 嵩明县| 特克斯县| 财经| 乌拉特前旗| 清水河县| 灯塔市| 潞城市| 华池县| 十堰市| 平和县| 保定市| 河西区| 堆龙德庆县| 东光县| 祁连县| 铁岭县| 罗甸县| 阜宁县| 玉树县| 修水县| 东光县| 茌平县| 互助| 济南市| 台北市| 康马县| 雅江县| 呼玛县| 澄迈县| 杭锦旗| 安阳县| 尚志市| 临潭县| 淅川县| 东乡族自治县| 香河县| 凤山县| 宜都市|