码迷,mamicode.com
首页 > 编程语言 > 详细

MyBatis笔记----SSM框架mybatis3整合springmvc spring4

时间:2017-04-06 10:05:12      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:localhost   ext   text   filter   html4   cdata   scan   java   har   

 

技术分享

 

 

 

 

 

 

 

 

技术分享

 

 

上节 无springmvc框架 http://www.cnblogs.com/tk55/p/6661786.html 

 

结构

技术分享

jar包

技术分享

 

web.xml 与index.jsp

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>mybatis_springmvc</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/ij34/mybatis/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextCleanupListener</listener-class> </listener> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingfilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingfilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping> </web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% response.sendRedirect("article/list"); %>

 

 

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--   自动扫描加载注解的包 -->
<context:component-scan base-package="com.ij34.bean"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp" ></property>
</bean>

</beans>

 

 

com.ij34.model

package com.ij34.model;

public class User {
  private int id;
  private String name;
  private int age;


  public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
public String toString() {
    return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}


}
package com.ij34.model;

public class Article {
  private int id;
  private User user;
  private String title;
  private String content;
  
public String getContent() {
    return content;
}
public void setContent(String content) {
    this.content = content;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

  
}
package com.ij34.model;

import java.util.List;

public interface UserMapper {
      
    public List<Article> selectarticle(int id);    
}

com.ij34.mybatis

 

UserMapper.xml

<?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.ij34.model.UserMapper">
<resultMap type="Article" id="resultAticleList">
  <id property="id" column="aid"/>
  <result property="title" column="title"/>
  <result property="content" column="content"/>
  <association property="user" javaType="User">
  <id property="id" column="id"/>
  <result property="name" column="name"/>
  <result property="age" column="age"/>
  </association>
  </resultMap>
  <select id="selectarticle" parameterType="int" resultMap="resultAticleList">
  select users.id,users.name,users.age,article.id aid,article.title,article.content from users,article
  where users.id=article.userid and users.id=#{id}
  </select>
  </mapper>

 

mybatis-config.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 type="com.ij34.model.Article" alias="Article"/>
<typeAlias type="com.ij34.model.User" alias="User"/>
</typeAliases>
</configuration>

 

applicationContext.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.3.xsd"
            default-autowire="byName" default-lazy-init="false"> 
    <!-- Showcase‘s CustomFreemarkerManager example -->
  <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
     <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property> 
     <property name="username" value="root"></property> 
     <property name="password" value="123456"></property> 
  </bean> 

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"></property>
      <property name="configLocation" value="classpath:com/ij34/mybatis/mybatis-config.xml"></property>
      <property name="mapperLocations" value="classpath:com/ij34/mybatis/UserMapper.xml"></property>
  </bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.ij34.model"></property>
</bean>
</beans>

 

 

com.ij34.bean

package com.ij34.bean;

import java.util.List;

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.ij34.model.Article;
import com.ij34.model.UserMapper;


@Controller
@RequestMapping("/article")
public class Test {
  
  @Autowired
  UserMapper mapper;
  @RequestMapping("/list") 
  public ModelAndView show(){//@RequestParam  请求参数   
     List<Article> articles=mapper.selectarticle(1);
     ModelAndView mav=new ModelAndView("list");
     mav.addObject("articles", articles);
    return mav;
 
  }
}

 

list.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<c:forEach var="article" items="${articles}">
<tr><td>${article.id} |</td><td> ${article.title}|</td><td> ${article.content}|</td><td>${article.user}</td> </tr>
</c:forEach>
</table>
</body>
</html

 

 

结果

技术分享

 

MyBatis笔记----SSM框架mybatis3整合springmvc spring4

标签:localhost   ext   text   filter   html4   cdata   scan   java   har   

原文地址:http://www.cnblogs.com/tk55/p/6671424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!