过滤器的作用,编译时候,resources下的*.propertise和*.xml没法编译到target里,所以这里添加过滤
<dependencies>
<!--mysql驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!--mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--junit-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
配置数据库资源
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=sz@mysql
1、引入properties文件
2、配置扫描的包(作用纯粹扫描xml,否则找不到对应的mapper.xml文件)
<mappers>****.xmlmappers>
如果是注解开发 需要在mapper里绑定接口 (这里我测试了不能同时扫描包和绑定接口,这就意味着 不能同时使用注解和xml开发,spring是可以的。也有可能是我搞错了吧)
<mappers>
<mapper class="com.mybatis.dao.UserMapper"/>
mappers>
3、设置别名
<typeAliases>******typeAliases>
这里如果不配置,那么mapper.xml的每个类都要写全路径,并且不配置的话, 没法使用注解开发(虽然不常用)。比如
配置前
<select id="getUserList" resultType="com.mybatis.pojo.User">
配置后
<select id="getUserList" resultType="User">
设置数据库环境(可以是多个 但是只能用一个)和要扫描的mapper(扫描某个文件或包下所有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>
<properties resource="db.properties"/>
<typeAliases>
<package name="com.mybatis.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="development1">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="sz@mysql"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--将接口绑定到配置-->
<!--方法1-->
<!--<mapper resource="com/mybatis/dao/UserMapper.xml"/>-->
<!--方法2-->
<!--<mapper resource="com/mybatis/dao/*.xml"/>-->
<!--方法3-->
<package name="com.mybatis.dao"/>
</mappers>
</configuration>
db.properties和在xml配置的数据库url 里面区别在于要不要转义符 &
package com.mybatis.pojo;
public class User {
private int id;
private String name;
private String sex;
private int age;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.mybatis.dao;
import com.mybatis.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> getUserList();
public void addUser(User user);
public void updateUserById(int id);
public void deleteUserById(int id);
}
<?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.mybatis.dao.UserMapper">
<select id="getUserList" resultType="User">
select * from user
</select>
<insert id="addUser" >
insert into user (id,name,sex,age) values(#{id},#{name},#{sex},#{age})
</insert>
<update id="updateUserById" parameterType="int">
update user set name="9999" where id=#{id}
</update>
<delete id="deleteUserById" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
package utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
private static SqlSession sqlSession;
static {
try{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (Exception e){
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
import com.mybatis.dao.UserMapper;
import com.mybatis.pojo.User;
import org.apache.ibatis.session.SqlSession;
import utils.MybatisUtil;
import java.util.List;
public class MyTest {
public static void main(String[] args) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
/**
* addUser
*/
// User user = new User();
// user.setId(9);
// user.setName("ttt");
// user.setSex("femal");
// user.setAge(88);
// mapper.addUser(user);
// sqlSession.commit();
/**
* select users
*/
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user.getName());
}
/**
* update user
*/
// mapper.updateUserById(1);
// sqlSession.commit();
/**
* delete user
*/
// mapper.deleteUserById(1);
// sqlSession.commit();
System.out.println("end");
sqlSession.close();
}
}