Maven+Spring+Spring MVC+Mybatis项目实战
1.建立mysql数据库和表
建立一个学生选课管理数据库。
表:学生表、班级表、教师表、课程表、学生选课表。
逻辑关系:每个学生有一个班级;每个班级对应一个班主任教师;每个教师只能当一个班的班主任;
使用下面的sql进行建数据库,先建立学生表,插入数据(2条以上)。
use student_manager;
create TABLE student_tbl(
STUDENT_ID VARCHAR(255) PRIMARY KEY,
STUDENT_NAME VARCHAR(100) NOT NULL,
STUDENT_SEX VARCHAR(10),
STUDENT_BIRTHDAY DATE,
CLASS_ID VARCHAR(255)
);
INSERT INTO STUDENT_TBL (STUDENT_ID,
STUDENT_NAME,
STUDENT_SEX,
STUDENT_BIRTHDAY,
CLASS_ID)
VALUES (123456,
'某某某',
'女',
'1980-08-01',
121546
)
select from student_tbl;
desc student_tbl;
insert into student_tbl values('111','Tom','男',DATE('2013-09-08'),'101');
insert into student_tbl values('222','小红','女',DATE('2014-02-08'),'101');
insert into student_tbl values('333','小明','男',DATE('2014-02-08'),'102');
insert into student_tbl values('444','小阳','女',DATE('2014-02-08'),'103');
创建连接MySql使用的配置文件mysql.properties。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=limingnihao&useUnicode=true&characterEncoding=UTF-8
Maven + Spring + Spring MVC + Mybatis 开发环境:
1.搭建Maven开发环境:
1.下载maven,安装后配置 高级-->系统变量 MAVEN_HOME:E:\Jay.He\apache-maven-3.0.5
2. path目录中添加:.;%MAVEN_HOME%/bin;
3. 验证Maven安装成功 :mvn -v或version
2.添加Spring和SpringMVC支持 -- web.xml
<!--Spring 的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Spring的启动监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring的log4j监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 字符集 过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<!-- Spring view转发器,提供Spring MVC支持 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>.do</url-pattern>
</servlet-mapping>
2. Spring的applicationContext的配置:
<!-- 导入属性配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--配置数据库连接属性,通过properties文件配置 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
</bean>
<!--配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--Spring整合Mybatis的配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--mybatis-configuration.xml配置文件位置-->
<property name="configLocation" value="classpath:mybatis-configuration.xml" />
<property name="dataSource" ref="dataSource" />
<!--dao接口对应的Mapper的xml文件位置-->
<property name="mapperLocations" value="classpath:he/maven/example/**/.xml" />
<!--扫描别名所在包 -->
<property name="typeAliasesPackage" value="he.maven.example.bean" />
</bean>
<!--配置Mybatis执行sql的模板 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!--扫描mappers文件,自动注入的包,一般是dao层(Mybatis起作用的地方) -->
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="he.maven.example.dao" />
</bean>
3.数据库连接的配置:
jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=root&useUnicode=true&characterEncoding=UTF-8
4.Spring MVC 文件dispatcher-servlet.xml的配置:
<!--以注解形式开发,并配置注解需要扫描的包 -->
<mvc:annotation-driven />
<context:component-scan base-package="he.maven.example" />
<!--Spring集成mybatis后的ORM配置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<property name="basePackage" value="he.maven.example" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--Spring MVC的控制规则 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>