1、创建web工程,导入spring和mybatis的jar包
2、建立开发目录结构,创建实体类
package com.zhiyou.pojo;
import java.util.Date;
public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
private String userRoleName; // 用户角色名称
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
@Override
public String toString() {
return "User [id=" + id + ", userCode=" + userCode + ", userName=" + userName + ", userPassword=" + userPassword
+ ", gender=" + gender + ", birthday=" + birthday + ", phone=" + phone + ", address=" + address
+ ", userRole=" + userRole + ", createdBy=" + createdBy + ", creationDate=" + creationDate
+ ", modifyBy=" + modifyBy + ", modifyDate=" + modifyDate + "]";
}
}
3、创建数据访问接口
package com.zhiyou.dao.user;
import java.util.List;
import com.zhiyou.pojo.User;
public interface UserMapper {
/**
* 查询用户列表(参数:对象入参)
* @return
*/
public List<User> getUserList(User user);
}
4、创建数据访问接口的实现类
package com.zhiyou.dao.user;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import com.zhiyou.pojo.User;
public class UserMapperImpl implements UserMapper {
@Override
public List<User> getUserList(User user) {
return sqlSession.selectList(
"com.zhiyou.dao.user.UserMapper.getUserList", user);
}
// 定义声明一个SqlSessionTemplate类属性
private SqlSessionTemplate sqlSession;
public SqlSessionTemplate getSqlSession() {
return sqlSession;
}
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
}
5、配置SQL映射语句文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhiyou.dao.user.UserMapper">
<!-- 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
<resultMap type="User" id="userList">
<result property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="phone" column="phone" />
<result property="birthday" column="birthday" />
<result property="gender" column="gender" />
<result property="userRole" column="userRole" />
<result property="userRoleName" column="roleName" />
</resultMap>
<!-- 查询用户列表(参数:对象入参) -->
<select id="getUserList" resultMap="userList" parameterType="User">
select u.*,r.roleName from smbms_user u,smbms_role r
where u.userName like CONCAT ('%',#{userName},'%')
and u.userRole = #{userRole} and u.userRole = r.id
</select>
</mapper>
6、配置mybatis应用配置文件–>基本没有用了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 给包名取别名,mapper映射文件写返回类型User的时候,会自动加上com.zhiyou.pojo -->
<typeAliases>
<package name="com.zhiyou.pojo"/>
</typeAliases>
</configuration>
7、配置spring应用配置文件
<?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:p="http://www.springframework.org/schema/p"
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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 配置数据库连接池 dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- <property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&characterEncoding=utf-8]]></value>
</property> -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?
useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 配置SQL映射文件信息 -->
<property name="mapperLocations">
<list>
<value>classpath:com/zhiyou/dao/**/*.xml</value>
</list>
</property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userMapper" class="com.zhiyou.dao.user.UserMapperImpl">
<property name="sqlSession" ref="sqlSessionTemplate" />
</bean>
<!-- 配置业务Bean -->
<bean id="userService" class="com.zhiyou.service.user.UserServiceImpl">
<property name="userMapper" ref="userMapper" />
</bean>
</beans>
8、写service业务逻辑层接口和实现类
package com.zhiyou.service.user;
import java.util.List;
import com.zhiyou.pojo.User;
public interface UserService {
public List<User> findUsersWithConditions(User user);
}
package com.zhiyou.service.user;
import java.util.List;
import org.springframework.stereotype.Service;
import com.zhiyou.dao.user.UserMapper;
import com.zhiyou.pojo.User;
/**
* @author: 波
* @date: 2019年4月23日
* @Desc: 实现类
*/
public class UserServiceImpl implements UserService {
// 定义声明一个UserMapper类属性
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public List<User> findUsersWithConditions(User user) {
try {
return userMapper.getUserList(user);
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
}
}
9、写Test测试类,运行程序测试
package com.zhiyou.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zhiyou.pojo.User;
import com.zhiyou.service.user.UserService;
/**
* @author: 波
* @date: 2019年4月23日
* @Desc: 测试
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
User user = new User();
user.setUserName("赵");
user.setUserRole(3);
List<User> list = userService.findUsersWithConditions(user);
//根据条件进行查询 参数是User
for (int i = 0; i < list.size(); i++) {
System.out.println("查到 " + list.size() + " 个结果");
System.out.println(list.get(i));
}
}
}