spring+springmvc+mybaties即SSM框架整合在ecpliseee中开发:很么多西都是只有只有自己上手做,才会懂。昨晚熬了很久,才弄出来。也希望对新手有帮助!
下面整理一下思路:
关键是各个配置文件!
首先是正确的导入jar包;
然后建立项目包;
完成的功能:
用户首先是进入登陆界面,按照正确的用户名和密码登陆进去,用户分为admin和guest,admin登陆进去对学生进行CRUD,guest登陆进去显示一个页面。
admin用户:
guest用户
关键在于配置几个文件
1.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"
default-autowire="byName">
<!-- 注入注解的支持 -->
<context:annotation-config></context:annotation-config>
<!-- 注入要扫描的组件包 -->
<context:component-scan base-package="com.mapper"></context:component-scan>
<context:component-scan base-package="com.biz"></context:component-scan>
<context:component-scan base-package="com.action"></context:component-scan>
<!-- 连接数据库的配置 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/emp">
</property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<!-- 注入mybaties的SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybaties-conf.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置mybaties的Mapper扫描包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"></property>
</bean>
<!-- 配置JDBC的事务管理器 -->
<bean id="txmanager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注解事务 -->
<tx:annotation-driven transaction-manager="txmanager"/>
</beans>
2.log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=error, stdout
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
### log schema export/update ###
#log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
3.mybaties-conf.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>
<mappers>
<mapper resource="com/mapper/StudentMapper.xml" />
<mapper resource="com/mapper/UserMapper.xml"/>
</mappers>
</configuration>
4.WEB-INF/springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"
default-autowire="byName"
>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.action"></context:component-scan>
</beans>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 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>
<!-- 中文编码配置 -->
<filter>
<filter-name>GBK</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>GBK</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Springmvc Servlet配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>springmvc</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<!-- springmvc映射 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 安全验证 -->
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
StudentMapper.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.mapper.IStudentMapper">
<!-- Student返回类型映射声明 -->
<resultMap type="com.po.Student" id="studentResultMap">
<id property="sid" column="sid"></id>
<result property="sname" column="sname"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
</resultMap>
<insert id="save" parameterType="com.po.Student">
insert into student(sname,sex,address) values(#{sname},#{sex},#{address})
</insert>
<update id="update" parameterType="com.po.Student">
update student set sname=#{sname},sex=#{sex},address=#{address} where sid=#{sid}
</update>
<delete id="delById" parameterType="java.lang.Integer">
delete from student where sid=#{sid}
</delete>
<select id="findById" parameterType="java.lang.Integer" resultType="com.po.Student">
select * from student where sid=#{sid}
</select>
<select id="findAll" resultType="com.po.Student" >
select * from student
</select>
</mapper>
UserMapper.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.mapper.IUserMapper">
<select id="check" parameterType="com.po.User" resultType="com.po.User">
select * from loginUser where uname=#{uname} and passwd=#{passwd}
</select>
</mapper>
数据库:
配置就说这么多,很代码最实际!提供给初次配置ssm的人员。
想要详细代码加qq:1242798356(私人)