1、工程目录
2、具体步骤
1、新建maven工程
修改pom.xml文件,添加相关依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vincent</groupId>
<artifactId>springMybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--mybatis依赖包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--mysql依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!--spring JDBC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--spring基础jar-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
</dependencies>
</project>
2、新建数据库文件
使用mysql新建一个简单的数据库文件,我的数据库名字设置为springmybatis
3、新建实体类
新建user.java,放在com.entity包下
package com.entity;
public class user {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "user [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
4、创建dao层
新建uesrMapper.java文件,放在com.dao包下,主要是各个功能的接口
package com.dao;
import com.entity.user;
public interface userMapper {
public user getUser(user user1);
public void addUser(user user1);
public void updateUser(user user1);
public void deleteUser(int userId);
}
5、创建mybatis配置文件
在resources文件夹下新建mybatisConfig.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>
<!--这里给实体类取别名,方便在mapper配置文件中使用 -->
<typeAlias alias="user" type="com.entity.user" />
</typeAliases>
<mappers>
<mapper resource="mapper/userMapper.xml" />
</mappers>
</configuration>
6.创建userMapper.xml实现文件
在mapper包下新建userMapper.xml,编写底层的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.dao.userMapper">
<select id="getUser" parameterType="user" resultType="user">
SELECT*
FROM springmybatis WHERE name=#{name} AND password=#{password}
</select>
<insert id="addUser" parameterType="user" flushCache="true">
INSERT INTO
springmybatis(id,name,password) VALUES
(#{id},#{name},#{password})
</insert>
<update id="updateUser" parameterType="user">
UPDATE springmybatis SET
password=#{password} WHERE id=#{id}
</update>
<delete id="deleteUser" parameterType="user">
DELETE FROM springmybatis WHERE
id=#{id}
</delete>
</mapper>
7、编写整合配置文件
编写springMybatis.xml文件,实现spring 与 mybatis的整合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 配置数据源 -->
<bean id="jdbcDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=UTF-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatisConfig.xml"></property>
</bean>
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.dao.userMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
8、编写测试文件
新建com.test包,在包下新建testa.java测试数据
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.userMapper;
import com.entity.user;
public class testa {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("springMybatis.xml");
userMapper up = (userMapper) ctx.getBean("userDao");
user user = new user();
// 添加两条数据
user.setId(20);
user.setName("Jcia");
user.setPassword("53");
up.addUser(user);
System.out.println("添加成功");
up.deleteUser(12);
System.out.println("删除成功");
}
}