eclipse ssm 环境搭建

来源:互联网 发布:辐射新维加斯优化 编辑:程序博客网 时间:2024/05/22 00:44

1.创建数据库:

-- ----------------------------
-- Table structure for `department`
-- ----------------------------

DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '唯一id',
  `name` varchar(25) DEFAULT NULL COMMENT '年龄',
  `is_ok` int(11) DEFAULT '1' COMMENT '是否有效',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of department
-- ----------------------------
INSERT INTO `department` VALUES ('1', '研发部', '1');
INSERT INTO `department` VALUES ('2', '销售部', '1');


-- ----------------------------
-- Table structure for `employee`
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '唯一id',
  `name` varchar(50) DEFAULT NULL COMMENT '姓名',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `department_id` int(11) DEFAULT NULL COMMENT '部门id',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO `employee` VALUES ('1', '张三', '20', '1');
INSERT INTO `employee` VALUES ('2', '李四', '20', '2');


2.导入基础jar包

http://download.csdn.net/detail/qq_21100733/9897811   基础jar包下载地址

3.创建项目

右键New -- Dynamic Web Project 



4.创建项目目录结构


5.创建实体类

Employee.java

public class Employee {
private Integer id;
private String name;
private Integer age;
private Integer department_id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 getDepartment_id() {
return department_id;
}
public void setDepartment_id(Integer department_id) {
this.department_id = department_id;
}

public Employee(Integer id, String name, Integer age, Integer department_id) {
super();
this.id = id;
this.name = name;
this.age = age;
this.department_id = department_id;
}
public Employee() {
super();
}

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", department_id=" + department_id + "]";
}

}

6.创建mapper接口以及mapper.xml文件

EmployeeMapper:


import org.springframework.stereotype.Component;


import com.ssm.po.Employee;


@Component("employeeMapper")
public interface EmployeeMapper {
Employee getEmpById(int id);
}

EmployeeMapper:

<?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.ssm.mapper.EmployeeMapper" >
<select id="getEmpById" parameterType="int" resultType="com.ssm.po.Employee">
        select * from employee where id=#{id}
    </select>
</mapper>


7.创建service

EmployeeService:

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

import com.ssm.mapper.EmployeeMapper;
import com.ssm.po.Employee;

@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;


public Employee getEmp(){
return employeeMapper.getEmpById(1);
}
}

8.创建control控制器

EmployeeControl:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;

import com.ssm.service.EmployeeService;
@Controller
@RequestMapping("/mvc")
public class EmployeeControl {

@Autowired
EmployeeService service;

@RequestMapping(value="/emp",method={RequestMethod.GET,RequestMethod.POST})
public String test(Model model){
model.addAttribute("emp", service.getEmp());
return "success";
}
}


9.创建配置文件

①创建数据库配置文件(db.properties)

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=mysql

②spring配置文件(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"
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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.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">
<!-- 加载指定properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置注解 -->
<context:component-scan base-package="com.spring.test" />
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<!-- 配置datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driverClassName}" />
<!-- 数据库URL -->
<property name="url" value="${jdbc.url}" />
<!-- 数据库用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 数据库密码 -->
<property name="password" value="${jdbc.password}" />
</bean>


<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- mybatis配置文件 -->
<property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"></property>  
</bean>


<!-- 加载mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径,如果需要扫描多个包,中间使用使用半角逗号隔开 -->
<property name="basePackage" value="com.ssm.mapper" />
<!-- 此属性在对应的类中定义存在为String类型,所传入值为String类型 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>


<!-- 扫描controller、service、... -->
<context:component-scan base-package="com.ssm.service"></context:component-scan>


<!-- 事务管理器 对mybatis操作数据库事务控制,spring使用JDBC的事务控制 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">


<property name="dataSource" ref="dataSource" />


</bean>

<!-- 通知给指定的事务管理器transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- aop调用 txAdvice -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.ssm.service.*.*(..))" />
</aop:config>

</beans>    

③ springmvc配置文件(springmvc.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"
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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.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">


<!-- 可以扫描controller、service、... 这里扫描controller,指定controller的包 -->
<context:component-scan base-package="com.ssm.control" />

<!-- 注解的映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 注解的适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>


<!-- 使用mvc:annotation-driven可以代替注解的适配器和注解映射器 mvc:annotation-driven默认加载了很多的参数绑定方法,比如json转换解析器就默认加载了 
实际开发时使用下方的mvc:annotation-driven -->


<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 (ViewResolver) 解析jsp视图,默认使用jstl标签,classpath下要有jstl的包 -->


<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 定义视图前缀和后缀 -->
<property name="prefix" value="/jsp/" />


<property name="suffix" value=".jsp" />
</bean>
</beans>


④mybatis 配置文件(SqlMapConfig.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="com.ssm.po" />
</typeAliases>
</configuration>


配置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">
  <display-name>SSMDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置前端控制器 -->
<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.xml</param-value>
</init-param>
</servlet>
<!-- 配置前端控制器映射器 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 加载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>
  
</web-app> 


10.创建jsp

success.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>
${emp.name }
</body>
</html>





原创粉丝点击