安装 SSM 开发环境并进行增删查改 demo
1、建立maven项目
2、输入信息
3、输入maven地址,并且添加一个新的 properties : archetypeCatalog = internal,可以加快速度
4、建立完成后,下载过程中会弹出一个框,选择 auto。
5、目录结构
bean: 放实体类
controller: 控制数据在哪个页面显示
dao:操作数据库
service:业务层
特别的,在新建项目时,需要右键 java ,选择 mark directory as source root,在 resource 右键 选择 mark directory as resource root.
6、导入一些老师准备的文件
applicationContext.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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.配置数据源 -->
<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}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描bean包 使用别名 -->
<property name="typeAliasesPackage" value="com.bean"></property>
<!--配置加载映射文件 UserMapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 自动生成dao,mapper-->
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.dao"/>
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--自动扫描-->
<context:component-scan base-package="com"/>
<!-- 配置事务-->
<!-- 5.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 6.开启事务注解-->
<tx:annotation-driven></tx:annotation-driven>
</beans>
db.properties 内容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=tiger
log4j.properties 内容如下:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
spring-mvx.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:mvc="http://www.springframework.org/schema/mvc"
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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 1.注解扫描位置-->
<context:component-scan base-package="com.controller" />
<!-- 2.配置映射处理和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 3.视图的解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
index.jsp 文件内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<html>
<body>
<h2>Hello World!</h2>
<a href="${pageContext.request.contextPath}/user/findAll.do">查询所有用户</a>
</body>
</html>
7、数据库结构
数据库 test,表名 userinfo
8、bean usrinfo 实体类
public class UserInfo {
private int id;
private String name;
private String password;
public UserInfo() {
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
9、controller UserInfoController 控制类
@Controller
@RequestMapping("user")
public class UserInfoController {
@Autowired
IUserInfoService userInfoService;
@RequestMapping("findAll.do")
public ModelAndView findAll() {
List<UserInfo> users = userInfoService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("users", users);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("toAddUser.do")
public String toAddUserInfo() {
return "addUser";
}
@RequestMapping("/save.do")
public String saveUserinfo(UserInfo userInfo) {
userInfoService.addUserInfo(userInfo);
return "redirect:findAll.do";
}
@RequestMapping("delete.do")
public String deleteUserInfo(int id) {
userInfoService.deleteUserInfo(id);
return "redirect:findAll.do";
}
@RequestMapping("toUpdate.do")
public String toUpdateUserInfo(UserInfo userInfo) {
return "updateUser";
}
@RequestMapping("update.do")
public String updateUserInfo(UserInfo userInfo) {
userInfoService.updateUserInfo(userInfo);
return "redirect:findAll.do";
}
}
10、service UserInfoService 接口
public interface IUserInfoService {
public List<UserInfo> findAll();
public void addUserInfo(UserInfo userInfo);
public void updateUserInfo(UserInfo userInfo);
public void deleteUserInfo(int id);
}
11、service UserInfoService 实现
@Service("userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {
@Autowired
IUserInfoDao userInfoDao;
@Override
public List<UserInfo> findAll() {
return userInfoDao.findAll();
}
@Override
public void addUserInfo(UserInfo userInfo) {
userInfoDao.addUserInfo(userInfo);
}
@Override
public void updateUserInfo(UserInfo userInfo) {
userInfoDao.updateUserInfo(userInfo);
}
@Override
public void deleteUserInfo(int id) {
userInfoDao.deleteUserInfo(id);
}
}
12、dao 层接口
public interface IUserInfoDao {
public List<UserInfo> findAll();
public void addUserInfo(UserInfo userInfo);
public void deleteUserInfo(int id);
public void updateUserInfo(UserInfo userInfo);
}
13、创建dao的mapper映射
是利用xml文件来直接实现dao层,直接在xml文件中写sql语句
namespace="com.dao.IUserInfoDao
表示操作的实体类
id="findAll"
表示dao类中的接口函数,对应的是 public List<UserInfo> findAll();
resultType
表示的是返回的类型,这里就是实体类 userinfo
<mapper namespace="com.dao.IUserInfoDao">
<select id="findAll" resultType="com.bean.UserInfo">
select * from userinfo
</select>
<select id="addUserInfo" parameterType="com.bean.UserInfo">
insert into userinfo values(#{id},#{name},#{password})
</select>
<select id="deleteUserInfo" parameterType="int">
delete from userinfo where id = #{id}
</select>
<select id="updateUserInfo" parameterType="com.bean.UserInfo">
update userinfo set name = #{name},password= #{password} where id =
#{id}
</select>
</mapper>
14、页面效果