前言
使用IDEA搭建一个ssm项目框架,并使数据可以传输到页面
创建一个普通的java项目
项目上右击,选择add framework...
按照以下勾选
在WEB-INF下新建文件夹为lib
将jar包复制都lib中,右键add as Lib...
下面的窗口直接点确定
点击exit...
点左上角的加号
到这里就可以直接点ok
一、实体类
创建数据库test1,创建表格t1,添加两个字段:id,name,以下是实体类的全部代码
package com.ssm;
public class Entity {
private Integer id;
private String name;
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;
}
@Override
public String toString() {
return "Entity{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public Entity() {
}
public Entity(Integer id, String name) {
this.id = id;
this.name = name;
}
}
二、mapper接口
使用注解的方式开发,写了一个全查的方法,仅作环境是否搭建成功测试,后续可以在mapper中添加其他方法。
package com.ssm;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface Mapper {
@Select("select * from t1")
List<Entity> findAll();
}
三、service接口
service接口中的抽象类就是mapper接口中的抽象类
package com.ssm;
import java.util.List;
public interface Service {
List<Entity> findAll();
}
四、service接口的实现类
service接口的实现类通过mapper接口的同名方法实现功能
package com.ssm.impl;
import com.ssm.Entity;
import com.ssm.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ServiceImpl implements com.ssm.Service {
@Autowired
private Mapper mapper;
@Override
public List<Entity> findAll() {
return mapper.findAll();
}
}
五、控制器
通过控制器控制页面的转发重定向和数据的传输
package com.ssm;
import com.ssm.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@org.springframework.stereotype.Controller
public class Controller {
@Autowired
private ServiceImpl s;
@RequestMapping("/findAll")
public ModelAndView findAll(){
ModelAndView view = new ModelAndView();
List<Entity> all = s.findAll();
view.addObject("all", all);//添加内容
view.setViewName("index");//目标页面
return view;
}
}
六、配置文件(src/config)
1.数据库文件(jdbc.properties)
此文件是数据库四要素的值
url = jdbc:mysql://localhost:3306/****?characterEncoding=utf-8
driver = com.mysql.jdbc.Driver
user = root
pswd = ****
applicationContext和spring-mvc的配置文件的文件头是一样的,只不过有些shema没有用到
2.spring核心配置(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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
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">
<!--配置包扫描(service)-->
<context:component-scan base-package="com.ssm"/>
<!--引入外部配置文件-->
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pswd}"/>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven proxy-target-class="true"/>
<!--spring和mybatis整合-->
<!--1.创建工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--配置别名-->
<property name="typeAliasesPackage" value="com.ssm"/>
</bean>
<!--2.配置mapper接口的代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm"/>
</bean>
</beans>
3.springmvc配置(spring-mvc.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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)-->
<context:component-scan base-package="com.ssm"/>
<!-- 打开mvc注解驱动-->
<mvc:annotation-driven/>
<!-- 开启静态资源访问权限-->
<mvc:default-servlet-handler/>
<!-- 配置前后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
七、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--指定spring配置文件的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<!--设置监听-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--指定springmvc的配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<!--随着服务器的启动而加载-->
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--任何请求都交由springmvc的前端控制器-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解决post提交乱码-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
项目目录
还有一个log4j.properties文件是定义日志输出格式,没有也不影响
运行结果