ssm框架搭建简单demo+源码
应某⽹络友⼈邀约,需要⼀个SSM框架的Demo作为基础学习资料,于是乎,就有了本⽂。⼀个从零开始的SSM框架Demo对⼀个新⼿来说,是⾮常重要的,可⼤⼤减少在学习过程中遇到的各种各样的坑,说到最后,也算是助⼈为乐吧!下⾯我们从零开始进⾏SSM框架的搭建,在介绍最后,百度云上提供有源码以便需要Demo的亲朋好友们进⾏下载~~~
环境配置
eclipse+tomcat
1*代码结构图⽰
2*配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:context="/schema/context"
xmlns:tx="/schema/tx" xmlns:aop="/schema/aop"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/aop
/schema/aop/spring-aop.xsd
/schema/tx
/schema/tx/spring-tx.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd">
<!-- 1.扫描器 -->
<context:component-scan base-package="com.fykj"></context:component-scan>
<!-- 2.⽇期转换器 -->
<!-- <bean id="formattingConversionServiceFactoryBean"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="verter.MyConverter"></bean>
</list>
</property>
<!--3 引⽤转换器 -->
<!-- <mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"></mvc:annotation-driven> --> <!-- 4.配置上传⽂件解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipartmons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10240000</value><!-- 最⼤上传尺⼨:102400B -->
</property>
</bean>
<!-- 5.将aop改成注解配置 -->
<!-- 打开代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!-- 整合配置 -->
<!-- 1.mybatis配置连接池 -->
<bean name="dataSource" class="hange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="sql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_test"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="maxPoolSize" value="10"></property>
<property name="minPoolSize" value="2"></property>
</bean>
<!-- 2.mybatis⼯⼚ -->
<bean name="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="l"></property>
</bean>
<!-- 3.mybatis中的mapper扫描 mapper(动态代理加强版) 扫描替换mybatis中的mapper扫描 -->
<bean name="mapperScanner" class="batis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fykj.mapper"></property>
</bean>
<!-- 4.配置事务管理 -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5.配置注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
3*主体代码
UserController
package ller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import ity.User;
import com.fykj.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
private UserService userService;
@RequestMapping("/login")
public String login(String username, String password, Model model) throws Exception { User user = userService.checkLogin(username, password);
if (user != null) {
model.addAttribute(user);
return "/welcome.jsp";
}
return "/fail.jsp";
}
}
entity
package ity;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
ssm框架简单吗}
public void setPassword(String password) {
this.password = password;
}
}
Service
user service
package com.fykj.service;
import ity.User;
public interface UserService {
public User checkLogin(String username, String password);
}
user service impl
package com.fykj.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import ansaction.annotation.Transactional;
import ity.User;
import com.fykj.mapper.UserMapper;
import com.fykj.service.UserService;
//@Service("userService")
//@Service
//@Transactional
@Component
public class UserServiceImpl implements UserService {
@Resource
public UserMapper userMapper;
public User checkLogin(String username, String password) {
User user = UserByName(username);
if (user != null && Password().equals(password)) {
return user;
}
return null;
}
}
mapper
user mapper
package com.fykj.mapper;
import ity.User;
public interface UserMapper {
public User getUserByName(String username);
}
user mapper xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fykj.mapper.UserMapper">
<!-- <resultMap type="ity.User" id="userMap"> -->
<!-- <id property="id" column="id" /> -->
<!-- <result property="username" column="username" /> -->
<!-- <result property="password" column="password" /> -->
<!-- </resultMap> -->
<select id="getUserByName" parameterType="String" resultType="ity.User"> select * from
db_user WHERE username=#{username}
</select>
</mapper>
源码下载地址:pan.baidu/s/1kVTKrJp
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论