SSM整合开发demo

SSM整合开发demo

一、前期准备

1.导入相关jar包
2.创建source folder源文件config,并创建db.properties、spring配置文件applicationContext.xml、以及mybatis配置文件mybatis-config.xml、springmvc的配置文件springmvc-config.xml
# db.properties文件配置
jdbc.driver=com.mysql.cj.jdbc.Driver
#jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:mysql://:3306/bybaitis?serverTimezone=UTC
#jdbc.url=jdbc:sqlserver://localhost:1433;databasename=eCommerce
jdbc.username=root
jdbc.password=123456
#新添加
jdbc.maxTotal = 30     
jdbc.maxIdle = 10
jdbc.initialSize = 5
#数据库连接最大数
<!-- 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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    <!-- 读取db.properties -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 -->
	<bean id="dataSource" 
                           class="org.apache.commons.dbcp2.BasicDataSource">
		<!--数据库驱动 -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<!--连接数据库的url -->
		<property name="url" value="${jdbc.url}" />
		<!--连接数据库的用户名 -->
		<property name="username" value="${jdbc.username}" />
		<!--连接数据库的密码 -->
		<property name="password" value="${jdbc.password}" />
		<!--最大连接数 -->
		<property name="maxTotal" value="${jdbc.maxTotal}" />
		<!--最大空闲连接  -->
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<!--初始化连接数  -->
		<property name="initialSize" value="${jdbc.initialSize}" />
	</bean>
     <!-- 事务管理器,依赖于数据源 -->
	<bean id="transactionManager" class=
     "org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>	
    <!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 配置MyBatis工厂SqlSessionFactory -->
    <bean id="sqlSessionFactory" 
                           class="org.mybatis.spring.SqlSessionFactoryBean">
         <!--注入数据源 -->
         <property name="dataSource" ref="dataSource" />
         <!--指定核MyBatis心配置文件位置 -->
  <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <!-- 配置mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="dao"/>
	</bean>
    <!-- 扫描Service --> 
    <context:component-scan base-package="service" />
</beans>

<!-- 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>
		<package name="po" />
	</typeAliases>
</configuration>

3.配置web.xml的信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  	xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
  	id="WebApp_ID" version="3.1">
  	<!-- 配置加载Spring文件的监听器-->
	<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>
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>
		     org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>
	<!-- 配置Spring MVC前端核心控制器 -->
	<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-config.xml</param-value>
		</init-param>
		<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
    	<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!--/:拦截所有请求(除了jsp)-->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

二、开始搭建

1.在src下创建po包,并创建持久化类Customer
package po;


public class Customer {

	private Integer id;
	private String username;
	private String jobs;
	private String phone;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getJobs() {
		return jobs;
	}
	public void setJobs(String jobs) {
		this.jobs = jobs;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
	}
	
	
}

2.在src下创建dao包,并创建CustomerDao接口文件和CustomerDao.xml映射文件
package dao;

import po.Customer;

public interface CustomerDao {

	public Customer findCustomerById(Integer 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="dao.CustomerDao">
   
   		<select id="findCustomerById" parameterType="Integer" resultType="Customer">
   			select * from customer where id = #{id}
   		</select>
   
   </mapper>
3.在src下创建service包并创建CustomerService接口文件并实现方法
package service;

import po.Customer;

public interface CustomerService {

	public Customer findCustomerById(Integer id);
	
}

4.在src下创建service.impl包并创建CustomerServiceImpl类
package service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.CustomerDao;
import po.Customer;
import service.CustomerService;


@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

	
	//注解注入CustomerDao
	@Autowired
	private CustomerDao customerDao;
	
	public Customer findCustomerById(Integer id) {
		
		//查询客户
		return this.customerDao.findCustomerById(id);
	}

}

5.在src下创建controller包并创建控制器类CustomerController
package controller;

import javax.json.Json;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import po.Customer;
import po.Result;
import service.CustomerService;

@RestController
public class CustomerController {

	//注解注入CustomerService
	@Autowired
	private CustomerService customerService;
	
	/*
	 * 根据id查询客户详情
	 */
	@RequestMapping(value = "/findCustomerById/{id}", produces = "application/json; charset=utf-8")
	public 	String findCustomerById(@PathVariable Integer id) {
		
		System.out.println(id);
		Customer customer = customerService.findCustomerById(id);
		System.out.println(customer);
		//返回客户信息展示页面
		Result result = new Result(0,"获取成功",customer);
		return result.toJSON();
	}
}

此处为restful风格,Result类为自定义放回格式的一个类

三、测试

在postman工具发送请求http://localhost:8080/containsNew/findCustomerById/3

返回案例

{
    "data": {
        "id": 3,
        "jobs": "worker",
        "phone": "45613513",
        "username": "tom"
    },
    "msg": "获取成功",
    "status": 0
}

gitee地址:https://gitee.com/jh1210

你可能感兴趣的:(java,ssm,mybatis,spring,java,后端)