1、配置mybatis-config(dao层)
在mybatis-config.xml中,我们只需要写类的别名和引用的Mapper
<?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>
<!--设置启用数据库字段下划线映射到java对象的驼峰式命名属性,默认为false-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--mapper.xml文件存放的包-->
<mappers>
<package name="com.document.mapper"/>
</mappers>
</configuration>
2、配置ApplicationContext.xml(service层)
在ApplicationContext.xml中,我们要配置数据库连接池,和sqlSessionFactory对象,以及Mapper对象。
<?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:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<context:component-scan base-package="com.document.service"/>
<!-- 数据连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/documentmang"></property>
<property name="username" value="root"></property>
<property name="password" value="djh123456"></property>
</bean>
<!-- 配置sqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- mapper包存放位置-->
<mybatis:scan base-package="com.document.mapper"></mybatis:scan>
</beans>
3、编写接口类与映射mapper.xml文件
接口UserMapper.java
public interface UserMapper {
//根据账号密码查询用户
User queryUserByAccAndPwd(@Param("user") User user);
//新增用户
int addUser(@Param("user") User user);
}
映射UserMapper.xml
<?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属性,不然会报错,可看做包名-->
<mapper namespace="com.document.mapper.UserMapper">
<select id="queryUserByAccAndPwd" parameterType="com.document.pojo.User" resultType="com.document.pojo.User">
select * from `user_info` where user_id = #{user.userId} and user_pwd = #{user.userPwd}
</select>
<insert id="addUser" parameterType="com.document.pojo.User" useGeneratedKeys="true" keyProperty="userId" >
insert into `user_info` values (null,#{user.userPwd},#{user.userName},#{user.phone},
#{user.email},#{user.sex},#{user.academicId},#{user.occupationId},0,default ,default ,default )
</insert>
</mapper>
4、编写业务类接口与实现类
接口
public interface UserService {
//根据账号密码查询用户
User queryUserByAccAndPwdService(User user);
//新增用户
int add(User user);
}
实现(实现类要打上@Service注解才能被spring识别)
@Service
public class UserServiceImpl implements UserService {
//自动注入mapper
@Autowired
private UserMapper userMapper;
//根据账号密码查询用户
@Override
public User queryUserByAccAndPwdService(User user) {
return userMapper.queryUserByAccAndPwd(user);
}
//新增用户
@Override
public int add(User user) {
return userMapper.addUser(user);
}
}
5、调用服务
//调用服务
UserService userService = ServiceFactory.getBean(UserService.class);
userService.add(user);
6、实体类(user)
@Data
public class User {
private int userId;
private String userPwd;
private String userName;
private String phone;
private String email;
private String sex;
private int academicId;
private int occupationId;
private int integralId;
private int status;
private String createTime;
private String updateTime;
}