刚使用idea其实还是很不习惯,原来都是习惯用于eclipse或者myeclipse。
现在用到ieda来搭建ssm项目的时候,发现一些问题。那么跟着博主一起来看看
首先我们在idea中 新建 maven项目
点击下一步,输入我们的模块名称和公司名称
再下一步,选择maven配置(这里我选择是默认的),再下一步直到完成
那么完成后来到这个页面,我们等待它的后台maven执行完成
接下来,我们在pom文件中导入 spring 和springMVC以及mybatis的相关依赖包
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.0.2.RELEASE</spring.version>
<jackson.version>2.5.0</jackson.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- mybatis 包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!--mybatis spring 插件 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- json -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSP -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
然后所有相关jar都导入进来,我们可以看到项目工程的一个目录结构
这里我们需要注意的是 main目录下只有一个webapp文件夹,这个用来存放我们编写的页面以及web.xml文件的。那么我们需要手动来创建java源代码的目录以及编写xml的配置文件目录
我们在main目录下新建java目录以及resource目录
接下来我们还需要做一些设置,让这两个文件夹能够起到对应的作用(不然是没用的)
选择File--->Project Structure...
打开之后的视图
展开右边的src目录的main,那么我们可以看到有java和resource
接下来,给这两个文件夹做操作,选中java 点击上面的图标,再选中resource点击上的图标
点完之后视图会发生改变 看下图 好了 到这里文件的操作完成了。接下来可以编写我们的java代码与xml文件了
1.在java文件夹下 新建目录 如下(分层结构)
我们先在pojo里面把实体类写好,以及mapper目录中把mybatis的sql写完
Dept类
package com.xt.pojo;
/**
* Dept实体类
* @author xz
*/
public class Dept {
private Integer deptNo;
public Integer getDeptNo() {
return deptNo;
}
public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
private String dName;
private String loc;
public Dept(Integer deptNo, String dName, String loc) {
this.deptNo = deptNo;
this.dName = dName;
this.loc = loc;
}
public Dept() {
}
@Override
public String toString() {
return "Dept{" +
"deptNo=" + deptNo +
", dName='" + dName + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
DeptMapper.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.xt.mapper.DeptMapper">
<!-- 配置resultMap -->
<resultMap id="deptResultMap" type="Dept">
<id column="deptno" property="deptNo"></id>
<result column="dname" property="dName"></result>
<result column="loc" property="loc"></result>
</resultMap>
<!--查询所有dept数据-->
<select id="getAll" resultMap="deptResultMap">
select * from dept
</select>
</mapper>
再来到resource目录下把数据库连接的配置文件写好以及导入log4j的配置文件
conn.properties (连接数据库配置信息)
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
user=scott
password=tiger
driverClass=oracle.jdbc.driver.OracleDriver
initialPoolSize=3
maxPoolSize=6
log4j.properties(日记信息)
# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=debug,stdout
# My logging configuration...
log4j.logger.cn.jbit.mybatisdemo=DEBUG
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
log4j.logger.org.apache.ibatis=DEBUG
## log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG
log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG
## log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
再编写mybatis主配置文件
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>
<typeAliases>
<!-- 指定别名 -->
<typeAlias type="com.xt.pojo.Dept" alias="Dept" />
</typeAliases>
</configuration>
接下来配置spring与mybatis相关类
spirng.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启扫描注解 -->
<context:component-scan base-package="com.xt">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
<!-- 加载 conn.properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:conn.properties"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:/mybatis.xml"></property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:/com/xt/mapper/*.xml"></property>
</bean>
<!-- 配置sessionTemplate 封装了繁琐的数据操作 -->
<bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!--事务管理-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务通知 -->
<tx:advice id="advice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务定义在service 边界-->
<aop:config>
<aop:advisor advice-ref="advice" pointcut="execution (* cn.xt.service.impl.*.*(..))"></aop:advisor>
</aop:config>
</beans>
再来配置spring-mvc.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"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.xt">
<!-- 问题....直接扫描 基础包 会影响事物功能 需要排除 service的注解 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!-- 开启mvc注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- WEB-INF下的所有文件以及文件夹 不提供给外直接访问 可以通过action 访问 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置文件上传项 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="1024000"></property>
</bean>
</beans>
spring与mybatis和spirngMVC的配置文件基本完成。接下来我们编写dao与service和controller的代码
dao包中我们新建接口 DeptDaoI
package com.xt.dao;
import com.xt.pojo.Dept;
import java.util.List;
/**
* dept操作的接口
*/
public interface DeptDaoI {
public List<Dept> getAll();
}
实现类 DeptDaoImpl
package com.xt.dao.impl;
import com.xt.dao.DeptDaoI;
import com.xt.pojo.Dept;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
/**
* 部门信息的dao实现类
*/
@Repository
public class DeptDaoImpl implements DeptDaoI {
@Resource
private SqlSessionTemplate sqlSessionTemplate;
@Override
public List<Dept> getAll() {
return sqlSessionTemplate.selectList("com.xt.mapper.DeptMapper.getAll");
}
}
再去service包中新建 service接口
package com.xt.service;
import com.xt.pojo.Dept;
import java.util.List;
/**
* 部门信息业务层接口
*/
public interface DeptServiceI {
public List<Dept> getAll();
}
service实现类
package com.xt.service.impl;
import com.xt.dao.DeptDaoI;
import com.xt.pojo.Dept;
import com.xt.service.DeptServiceI;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 部门信息的业务层 实现类
*/
@Service
public class DeptServiceImpl implements DeptServiceI {
@Resource
private DeptDaoI deptDao;
@Override
public List<Dept> getAll() {
return deptDao.getAll();
}
}
然后再去controller包中编写controller控制访问类
package com.xt.controller;
import com.xt.pojo.Dept;
import com.xt.service.DeptServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class DeptController {
@Resource
private DeptServiceI deptService;
@RequestMapping("/index")
public String index(){
System.out.print("hello index");
return "redirect:getAll.action";
}
@RequestMapping("/getAll")
public String getAll(){
List<Dept> list = deptService.getAll();
System.out.print(list);
return "all";
}
}
到此,java代码也整合完毕。
接下来,还有web.xml文件需要配置。因为我们要通过tomcat启动来初始化spring的IOC容器以及springMVC的配置
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>
<!-- application的初始化参数 加载spring配置文件,启动IOC容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springMVC配置,启动tomcat启动该servlet 同时加载spring-mvc的配置文件 -->
<servlet>
<servlet-name>mvc</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
整个项目目录结构如下
好了,到此 框架整合完毕,接下来我们试着运行下 项目,并且访问测试是否配置都正确 (配置tomcat此处不做说明)
启动后观察控制台有无报错信息,如无则说明启动成功,如下图
接着访问下项目地址,页面能够访问到了,并且工具控制台能够看到数据 (数据没有在页面中展示)
在idea中整合ssm框架完毕
注意:在pom文件中的 build 一定要加下列代码 不然启动报错或者访问的时候报错 是因为加载不到 java文件夹下的 mapper文件夹的mapper文件
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>