目前基于JavaEE的企业级MVC开源框架SSM,是当今比较主流的开发框架,SSM出来的时间相对较晚,相对于SSH来说更加的轻量和简单易学,同时更是互联网应用开发的不二选择。今天我就SSM框架的整合中Spring+Mybatis整合做一个简单整理。
jdk8.0+tomcat8.0+IDEA+MySql数据库
在idea中建立maven工程sm_demo1,并进行如下配置:(idea中jar配置坐标后会自动下载,此处不多做解释)
pom.xml:坐标配置
src:该目录下主要存放java代码,其中:
com.lmm.sm.bean:管理java实体类;
com.lmm.sm.dao:持久层代码管理,主要完成对数据库的CRUD;
com.lmm.sm.service:业务层代码管理,主要完成相关的业务处理;;
com.lmm.sm.test:一般用来进行单元测试;
resources:该目录下主要存放配置文件,其中有:
db.properties:配置数据库的driver、url、username、password
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
log4j.properties:配置框架日志输出
# Global logging configuration
# developer-->DEBUG productor-->INFO or ERROR
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
6
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# 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
mybatis-config.xml:主要进行mybatis框架的配置,我们一般会配置别名、mapper映射等
spring-bean.xml:spring框架配置文件,主要用来配置dataSource、sqlSessionFactory等
/**
* 用户实体类
*/
public class User implements Serializable {
private Integer id;
private String username;
private String sex;
private Date birthday;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
", address='" + address + '\'' +
'}';
}
}
/**
* dao层接口
*/
public interface UserDao {
@Select("select *from user")
@Results(
id = "userMap",
value = {
@Result(id = true,column = "id",property = "id"),
@Result(column = "username",property = "username"),
@Result(column = "sex",property = "sex"),
@Result(column = "birthday",property = "birthday"),
@Result(column = "address",property = "address")
}
)
public List findUsers();
}
/**
* service层实现类
*/
@Service("userService")//需要在spring-dao.xml中配置扫包等同于
public class UserServiceImpl implements UserService {
@Autowired//需要在spring-dao.xml中配置相关信息,等同于
private UserDao userDao;
public List findUsers() {
return userDao.findUsers();
}
}
/**
* service层接口
*/
public interface UserService {
/**
* 查询所有用户信息
* @return
*/
public List findUsers();
}
public class UserTest {
/**
* 查询所有用户信息
*/
@Test
public void findUsers(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-bean.xml");
UserService userService = (UserService) context.getBean("userService");
List users = userService.findUsers();
System.out.println(users);
}
}
到这里,我们的mybatis+spring框架的整合就完成了,后续我们还可以加上spring-mvc的整合等。