1、创建数据库表,以员工信息表为例子:
DROP TABLE IF EXISTS `em_info`; CREATE TABLE `em_info` ( `em_id` INT(50) NOT NULL AUTO_INCREMENT COMMENT '员工ID', `em_name` VARCHAR(100) NOT NULL COMMENT '员工姓名', `em_sex` VARCHAR(30) NOT NULL COMMENT '性别', `em_birthday` DATE NOT NULL COMMENT '出生日期', `em_hiredate` DATE NOT NULL COMMENT '入职日期', `em_salary` DOUBLE NOT NULL COMMENT '工资', PRIMARY KEY (`em_id`) )ENGINE=INNODB DEFAULT CHARSET=utf8mb4; INSERT INTO `em_info` VALUES (NULL,'小李飞刀','男','1999-2-8','2019-6-6','9999');
2、创建maven工程
2.1创建Module(web、service、dao、domain、utils子模块)
注意创建web子模块需要选择骨架,如下:
2.2、在domain子模块下创建实体类员工信息表Em_info,并提供get和set方法
2.3、DAO下创建IEm_infoDao接口,写查找方法findAll()。需要连续两次快捷键Alt+Enter,添加依赖和导入,导包才消除红色不报错
2.4、同2.3步奏,service下创建IEm_infoService接口,写查找方法findAll()。需要连续两次快捷键Alt+Enter,添加依赖和导入,导包才消除红色不报错。
2.5创建service实体类,实现接口,重写方法
2.6在web.Mian包下手动创建java和resources两个包并指定为源码包和资源部
在resouces包下创建applicationContext.xml和spring-mvc.xml文件
2.7applicationContext.xml导入约束
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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"> </beans>
2.7.1、在添加下面配置扫描service和dao和配置连接池
<!-- 开启注解扫描,管理service和dao --> <context:component-scan base-package="com.fzj.ssm.service"> </context:component-scan> <context:component-scan base-package="com.fzj.ssm.dao"> </context:component-scan> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 把交给IOC管理 SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dat