搭建ssm

一、建立maven工程
java

二、導入依賴mysql

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
	  <parent>
			<groupId>cn.itcast.parent</groupId>
			<artifactId>itcast-parent</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</parent>
  <groupId>cn.internet.userinfo</groupId>
  <artifactId>internet-userinfo</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
		<!-- 單元測試 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>

		<!-- Mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
		</dependency>

		<!-- 通用Mapper -->
		<dependency>
			<groupId>com.github.abel533</groupId>
			<artifactId>mapper</artifactId>
		</dependency>

		<!-- 分頁助手 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.jsqlparser</groupId>
			<artifactId>jsqlparser</artifactId>
		</dependency>

		<!-- MySql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

		<!-- 日誌 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>

		<!-- Jackson Json處理工具包 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>

		<!-- 鏈接池 -->
		<dependency>
			<groupId>com.jolbox</groupId>
			<artifactId>bonecp-spring</artifactId>
		</dependency>

		<!-- Apache工具組件 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- http://127.0.0.1:${port}/${path} -->
					<port>80</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

三、加入配置文件git

wex.xmlgithub

<?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="MyWebApp" version="2.5">
   <display-name>internet-userinfo</display-name>
 
   <!-- 啓動spring -->
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext*.xml</param-value>
   </context-param>
   <!-- 使用監聽器加載applicationContext文件 -->
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
 
   <!-- 加載springMVC -->
   <servlet>
      <servlet-name>itcast-userinfo</servlet-name>
      <!-- 配置DispatcherServlet,springmvc入口 -->
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:spring/internet-userinfo-servlet.xml</param-value>
      </init-param>
      <!-- 啓動web容器就啓動springMVC -->
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>itcast-userinfo</servlet-name>
      <!-- 全部的請求都進入springMVC -->
      <url-pattern>/</url-pattern>
   </servlet-mapping>
 
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
 
</web-app>

mybatis-config.xmlweb

<?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>
	
	<settings>
		<!-- 開啓駝峯自動映射 -->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
	
	<plugins>
	
		<!-- com.github.pagehelper爲PageHelper類所在包名 -->
		    <plugin interceptor="com.github.pagehelper.PageHelper">
		        <property name="dialect" value="mysql"/>
		        <!-- 設置爲true時,使用RowBounds分頁會進行count查詢 -->
		        <property name="rowBoundsWithCount" value="true"/>
		        <property name="reasonable" value="true"/>
		    </plugin>
		
	
		  <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
		    <!--主鍵自增回寫方法,默認值MYSQL,詳細說明請看文檔-->
		    <property name="IDENTITY" value="MYSQL"/>
		    <!--通用Mapper接口,多個通用接口用逗號隔開-->
		    <property name="mappers" value="com.github.abel533.mapper.Mapper"/>
		  </plugin>
	</plugins>
	
</configuration>

applicationContext.xmlspring

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 使用spring自帶的佔位符替換功能 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 容許JVM參數覆蓋 -->
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<!-- 忽略沒有找到的資源文件 -->
		<property name="ignoreResourceNotFound" value="true" />
		<!-- 配置資源文件 -->
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>
	
	<context:component-scan base-package="cn.internet" />

	<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
		destroy-method="close">
		<!-- 數據庫驅動 -->
		<property name="driverClass" value="${jdbc.driver}" />
		<!-- 相應驅動的jdbcUrl -->
		<property name="jdbcUrl" value="${jdbc.url}" />
		<!-- 數據庫的用戶名 -->
		<property name="username" value="${jdbc.username}" />
		<!-- 數據庫的密碼 -->
		<property name="password" value="${jdbc.password}" />
		<!-- 檢查數據庫鏈接池中空閒鏈接的間隔時間,單位是分,默認值:240,若是要取消則設置爲0 -->
		<property name="idleConnectionTestPeriod" value="60" />
		<!-- 鏈接池中未使用的連接最大存活時間,單位是分,默認值:60,若是要永遠存活設置爲0 -->
		<property name="idleMaxAge" value="30" />
		<!-- 每一個分區最大的鏈接數 -->
		<property name="maxConnectionsPerPartition" value="150" />
		<!-- 每一個分區最小的鏈接數 -->
		<property name="minConnectionsPerPartition" value="5" />
	</bean>

</beans>

springmvc配置文件sql

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 註解掃描 -->
	<mvc:annotation-driven />

	<!-- 配置controller掃描 -->
	<context:component-scan base-package="cn.internet.userinfo.controller" />


	<!-- 配置視圖解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前綴 -->
		<property name="prefix" value="/WEB-INF/views/"></property>
		<!-- 後綴 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

mybatis數據庫

applicationContext-mybatis.xmlapache

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 配置sqlSessionFactory -->
		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<!-- 配置數據源 -->
			<property name="dataSource" ref="dataSource"></property>
			<!-- mybatis全局配置文件 -->
			<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
			<!-- 加載mapper.xml -->
			<!-- <property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml"></property> -->
			<!-- 別名包 -->
			<property name="typeAliasesPackage" value="cn.internet.userinfo.pojo"></property>
		</bean>

	<!-- 掃描Mapper -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.internet.userinfo.mapper"></property>
	</bean>

</beans>

四、使用maven插件啓動Tomcatspring-mvc

五、加入pojo

package cn.internet.userinfo.pojo;

import java.util.Date;


import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
 * 用戶實體
 * @author zhangbin
 *
 */
@Table(name="tb_user")
public class User {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    // 用戶名
    private String userName;

    // 密碼
    private String password;

    // 姓名
    private String name;

    // 年齡
    private Integer age;

    // 性別,1男性,2女性
    private Integer sex;

    // 出生日期
    private Date birthday;

    // 建立時間
    private Date created;

    // 更新時間
    private Date updated;

    public Long getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public String toString() {
        return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", name=" + name
                + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created
                + ", updated=" + updated + "]";
    }
    

}

六、編寫mapper

public interface UserMapper extends Mapper<User> {
 
}

七、編寫service

@Service
public class UserService {
 
    @Autowired
    private UserMapper userMapper;
 
    /**
     * 根據id查詢用戶數據
     * 
     * @param id
     * @return
     */
    public User queryUserById(Long id) {
        return this.userMapper.selectByPrimaryKey(id);
    }
 
    /**
     * 分頁查詢用戶數據
     * 
     * @param page
     * @param rows
     * @return
     */
    public List<User> queryListByPage(Integer page, Integer rows) {
        // 第一個參數是頁碼,第二個參數是每頁顯示數據條數
        PageHelper.startPage(page, rows);
        return this.userMapper.select(null);
    }
 
}

八、編寫controller

@RequestMapping("user")
@Controller
public class UserController {
 
    @Autowired
    private UserService userService;
 
    /**
     * 根據id查詢用戶數據
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public User queryUserById(@PathVariable("id") Long id) {
 
        User user = this.userService.queryUserById(id);
        return user;
 
    }
 
    /**
     * 分頁查詢用戶數據
     * 
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public List<User> queryListByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "2") Integer rows) {
        List<User> list = this.userService.queryListByPage(page, rows);
        return list;
    }
 
}
相關文章
相關標籤/搜索