SSM环境搭建——简单实现SSM整合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011897110/article/details/78560208

一、项目架构


一、导入相关jar包

Jar包:链接:http://pan.baidu.com/s/1boGIG2N 密码:1wh8

二、配置相关信息

Bean:对应

public class User {
	private int id;
	private String username;
	private String password;
}
jdbc.properties:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/hgjs
username=root
password=123456

web.xml:

<!-- Spring配置文件开始 -->
<!-- 加载Spring容器配置 -->
<listener> 
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener>
	
<!-- Spring和mybatis的配置文件 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:spring-mybatis.xml</param-value>
	</context-param>
<!-- Spring配置文件结束 -->

<!-- SpringMVC配置文件开始 -->
	<!-- 添加对SpringMVC的支持 -->
	<!-- 配置核心控制器,拦截匹配的请求,分发到具体的控制器,完成公共功能。 -->
<servlet>
	<servlet-name>springMVC</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 指定Spring容器初始化配置文件路径 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-mvc.xml</param-value>
	</init-param>
	<!-- DispatcherServlet初始化比较耗时,所以在tomcat容器启动的时候就对这个Servlet进行初始化 DispatcherServlet初始化的时候,会自动初始化Spring容器。那Spring容器初始化的时候就需要配置文件 
		默认的配置文件查找的是/WEB-INF/<servlet-name>-servlet.xml -->
	<load-on-startup>1</load-on-startup>
	<!-- 异步处理功能 1.使用asyncSupported=true必须运用tomcat7+JDK6以上版本。 2.必须在一个请求涉及的所有Servlet及Filter中都声明asyncSupported=true。 -->
	<!-- <async-supported>true</async-supported> -->
</servlet>

<servlet-mapping>
	<servlet-name>springMVC</servlet-name>
	<url-pattern>*.do</url-pattern>

</servlet-mapping>

spring-mybatis.xml:

<!-- 自动扫描 -->
<context:component-scan base-package="org.zl" />

<!-- ************************************************************************************************** -->

<!-- 引入配置文件 -->
<bean
	class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
	<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 配置数据源 -->
<bean id="dataSource"
	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="${driverClassName}" />
	<property name="url" value="${url}" />
	<property name="username" value="${username}" />
	<property name="password" value="${password}" />
</bean>

<!-- ************************************************************************************************** -->

<!-- 事务管理 -->
<bean id="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用声明式事务 transaction-manager:引用上面定义的事务管理器 -->
<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" /> -->
	<!-- 自动扫描mapping.xml文件 -->
	<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 ,包下的类需要使用@MapperScan注解,否则容器注入会失败 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 给出需要扫描Dao接口包 -->
	<property name="basePackage" value="org.zl.dao" />
	<!-- 注入sqlSessionFactory 这句话可写可不写,因为spring会去自动查找并注入 -->
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

spring-mvc.xml:

<context:component-scan base-package="org.zl.controller" />

<!-- 视图解析器 -->
<bean id="viewResolver"
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<!-- <property name="prefix" value="/console/"/> -->
	<property name="prefix" value="/" />
	<property name="suffix" value=".jsp" />
</bean>

Dao:

@Repository("userDAO")
public interface UserDAO {
	public User getUserById(int id);
}

mapper:

<mapper namespace="org.zl.dao.UserDAO">
	<!-- public User getUserById(int id); -->
	<select id="getUserById" resultType="org.zl.bean.User">
		select * from user
		where
			id = #{id}
	</select>
</mapper>

service:

public interface IUserService {
	public User getUserById(int id);
}

servicelmpl:

@Service
public class UserServiceImpl implements IUserService {

	@Resource
	private UserDAO userDAO;
	
	public User getUserById(int id) {
		return userDAO.getUserById(id);
	}
}

三、测试

UserTest:

public class UserTest {
	private static UserDAO userDAO;

    public static void main(String[] args) {  
        //加载容器  
	ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml");  
        //bean的名字与接口名相同但首字母小写  
        userDAO = (UserDAO) context.getBean("userDAO");  
        User user = userDAO.getUserById(1);
        System.out.println(user.getUsername() +"****"+user.getPassword()+"***"+user.getId());  
    }  
}

Controller:

@Controller
public class UserController {
	@Resource
	private IUserService iUserService;
	
	@RequestMapping("/userid.do")
	public String getUserId(HttpServletRequest request, Model model) {
		int id = Integer.parseInt(request.getParameter("id"));

	User user = this.iUserService.getUserById(id);
	model.addAttribute("user", user);
	System.out.println(user.getUsername() + ":" + user.getId() + "!!!");
	return "login";
	}
}

jsp:  UserTest.jsp

<form action="userid.do" method="post">
	id:<input type="text" name="id"/><br/>
	<input type="submit" value="提交"><br>
</form>
jsp:Login.jsp
<body>
	SSM整合成功
</body>

展开阅读全文

没有更多推荐了,返回首页