演示一个简单的SSM框架搭建流程,(所用内容:Spring+SpringMVC+Mybatis+Dbcp数据源+log4j(mybatis使用))
搭建好的框架:http://pan.baidu.com/s/1dZuEy
主要用到的包
1.Spring框架 + Springmvc包
2.Springmvc+mybatis整合包 (mybatis提供,之前由Spring提供) mybatis-spring.1.2.2.jar
3.Mybatis包
4.Dbcp包
5.Junit包
步骤
1.拷贝所需要的包到WEB-INF/lib 文件夹下面, 并Add classpath
2.配置数据库: 利用配置文件 db.properties
3.配置log4j 实用配置文件 log4j.properties
4.配置SpringMVC (内含Dbcp配置)
5.配置Mybatis
6.编写测试类 方法测试是否完成搭建,
7.数据库表 user表
8.User 数据展示
9.最终测试结果
文档目录结构
1.拷贝所需要的包到WEB-INF/lib 文件夹下面
2.配置数据库: 利用配置文件 db.properties
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
3.配置log4j 实用配置文件 log4j.properties
log4j.properties
# Global logging configuration
#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216error
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
4.配置SpringMVC (内含Dbcp配置) applicationContext.xml
5.配置Mybatis SqlMapConfig.xml
6.编写测试类 方法测试是否完成搭建,
user.java
package cn.itcast.ssm.po;
import java.util.Date;
import java.util.List;
//hibernate字段名和属性名相对应
public class User {
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", birthday="
+ birthday + ", sex=" + sex + ", address=" + address + "]";
}
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
// 用户的订单商品信息
List orderlist;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
// 用户的订单商品信息
public List getOrderlist() {
return orderlist;
}
public void setOrderlist(List orderlist) {
this.orderlist = orderlist;
}
}
package cn.itcast.ssm.dao;
import java.util.List;
import cn.itcast.ssm.po.User;
public interface UserDao {
// 根据id查询用户信息
User findUserById(int id) throws Exception;
// 根据用户名列查询用户列表
public List findUserByName(String name) throws Exception;
// 添加用户信息
void insertUser(User user) throws Exception;
// 删除用户信息
void deleteUser(int id) throws Exception;
}
UserDaoImpl.java
package cn.itcast.ssm.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.itcast.ssm.po.User;
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override
public User findUserById(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
User user = null;
try {
user = sqlSession.selectOne("test.findUserById", id);
} catch (Exception e) {
e.printStackTrace();
}
// 因为利用了Spring进行管理,Spring会自动关闭SqlSession
return user;
}
@Override
public List findUserByName(String name) throws Exception {
// 因为利用了Spring进行管理,Spring会自动关闭SqlSession
SqlSession sqlSession = this.getSqlSession();
List userList = null;
try {
userList = sqlSession.selectList("test.findUserByName", name);
} catch (Exception e) {
e.printStackTrace();
}
return userList;
}
@Override
public void insertUser(User user) throws Exception {
// 因为利用了Spring进行管理,Spring会自动关闭SqlSession
SqlSession sqlSession = this.getSqlSession();
try {
sqlSession.insert("test.insertUser", user);
// //使用Spring管理Mybatis,不需要自动提交,自动提交会报错
// // java.lang.UnsupportedOperationException: Manual commit is not
// sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void deleteUser(int id) throws Exception {
// 因为利用了Spring进行管理,Spring会自动关闭SqlSession
SqlSession sqlSession = this.getSqlSession();
try {
sqlSession.delete("test.deleteUser", id);
// sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
SELECT LAST_INSERT_ID()
INSERT INTO user(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})
DELETE FROM user WHERE id=#{id}
UPDATE user SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address}
WHERE id=#{id}
需要在 SqlMapConfig.xml 中加载User.xml (已配置好)
需要在 applicationContext.xml 中配置SessionFactory注入到DaoImpl中
Junit 测试代码
package cn.itcast.ssm.dao;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.ssm.po.User;
public class UserDaoImplTest {
private ApplicationContext applicationContext;
// 此方法是在执行testFindUserById之前执行
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext(
"classpath:spring/applicationContext.xml");
}
@Test
public void testFindUserById() throws Exception {
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
// applicationContext.getAliases(arg0)
User user = userDao.findUserById(1);
System.out.println(user);
}
@Test
public void testFindUserByName() throws Exception {
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
List userList = userDao.findUserByName("小");
System.out.println(userList);
}
@Test
public void testInsertUser() throws Exception {
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
User user = new User();
user.setUsername("hahaha");
userDao.insertUser(user);
}
@Test
public void testDeleteUser() throws Exception {
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.deleteUser(40);
}
}
7.数据库表 user表
8.User 数据展示
9.最终测试结果
利用Junit 测试删除方法