相关jar包下载【链接:https://pan.baidu.com/s/1qJg8VjwnAqZt0GEq6Z258A?pwd=5hwd
提取码:5hwd 】
复制这段内容后打开百度网盘手机App,操作更方便哦
1、创建数据库并插入几条数据
CREATE TABLE `t_user` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(255) NULL ,
`jobs` varchar(255) NULL ,
`phone` varchar(255) NULL ,
PRIMARY KEY (`id`)
)
;
2、创建db.properties文件
maxTotal:最大连接数,maxIdle:最大空闲连接数,initialSize:初始化连接数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mybatis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
3、创建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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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">
<!-- 读取db.properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 连接数据库的url -->
<property name="url" value="${jdbc.url}" />
<!-- 连接数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 连接数据库的密码 -->
<property name="password" value="${jdbc.password}" />
<!-- 最大连接数 -->
<property name="maxTotal" value="${jdbc.maxTotal}" />
<!-- 最大闲置连接 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!-- 初始化连接数 -->
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!-- 事务管理器,依赖于数据源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 注册事务管理器驱动,开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置mybatis工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 指定核心配置文件位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 实例化Dao -->
<bean id="userDao" class="com.ssm.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- mapper代理开发(基于MapperFactoryBean) -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.ssm.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- mapper代理开发(基于MapperScannerConfigurer) -->
<!--
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.mapper" />
</bean>
-->
</beans>
3、mybatis-config.xml
<?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>
<!-- 配置别名 -->
<typeAliases>
<package name="com.ssm.po" />
</typeAliases>
<!-- 配置mapper的位置 -->
<mappers>
<mapper resource="com/ssm/mapper/UserMapper.xml" />
</mappers>
</configuration>
4、在com.ssm.po包中创建User
package com.ssm.po;
public class User {
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getJobs() {
return jobs;
}
public String getPhone() {
return phone;
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
}
}
5、在com.ssm.mapper下创建UserMapper接口及对应的映射文件(名字要相同)
package com.ssm.mapper;
import com.ssm.po.User;
public interface UserMapper {
//查找
public User findUserById(Integer id);
//添加
public int insertUser(User user);
//更新
public int updateUserById(User user);
//删除
public int deleteUser(Integer id);
}
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="com.ssm.mapper.UserMapper">
<!-- 通过编号获取信息 -->
<select id="findUserById" parameterType="Integer" resultType="com.ssm.po.User">
select * from t_user where id=#{id}
</select>
<!-- 添加用户信息 -->
<insert id="insertUser" parameterType="com.ssm.po.User" >
insert into t_user(username,jobs,phone) values(#{username},#{jobs},#{phone})
</insert>
<!-- 更新用户信息 -->
<update id="updateUserById" parameterType="com.ssm.po.User" >
update t_user set username=#{username},jobs=#{jobs},phone=#{phone} where id=#{id}
</update>
<!-- 删除用户信息 -->
<delete id="deleteUser" parameterType="Integer" >
delete from t_user where id=#{id}
</delete>
</mapper>
select元素要有parameterType属性以及resultType属性,因为查完返回查到的信息。
而insert、update、delete不需要resultType属性。还要注意这几个元素的parameterType属性值,传入的语句参数不同。最重要的是要保证他们的id要与你在UserMapper中写的方法名一致,最好复制粘贴。
Spring_MyBatis_Test测试类
package com.ssm.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ssm.dao.UserDao;
import com.ssm.mapper.UserMapper;
import com.ssm.po.User;
public class Spring_MyBatis_Test {
@Test
public void findUserByIdTest(){
//1、初始化spring容器,加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//2、通过容器获得userDao实例
UserDao userDao=(UserDao)applicationContext.getBean("userDao");
//调用UserDao接口查询方法(id为2)
User user=userDao.findUserById(2);
System.out.println(user.toString());
}
@Test
public void findUserByIdTest2(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper");
User user=userMapper.findUserById(2);
System.out.println(user);
}
@Test
public void insertUserTest(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper");
User user=new User();
user.setUsername("hqx2");
user.setJobs("teaccher");
user.setPhone("123456789");
userMapper.insertUser(user);
}
@Test
public void insertUserTest1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//2、通过容器获得userDao实例
UserDao userDao=(UserDao)applicationContext.getBean("userDao");
User user=new User();
user.setUsername("hqx1");
user.setJobs("teaccher");
user.setPhone("123456789");
userDao.insertUser(user);
}
@Test
public void updateUser(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper");
User user=new User();
user.setUsername("11");
user.setJobs("11");
user.setPhone("11");
user.setId(2);
userMapper.updateUserById(user);
}
@Test
public void deleteUserTest(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper");
userMapper.deleteUser(2);
}
}
通过Dao接口方式开发与Mapper接口方式开发都能够实现,读者可自行测试。
但Mapper的开发方式更加简洁,代码比其少。
以下是一些注意事项:
1、在Mapper开发时,要保证Mapper接口名与Mapper.xml映射文件名一致
2、要将Mapper.xml文件的namespace与Mapper接口类路径一致(接口与映射文件要在一个包下)
项目总体架构