1.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>
< properties resource = " db.properties" />
< 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>
</ environments>
< mappers>
< mapper resource = " databases/AdministratorMapper.xml" />
< mapper resource = " databases/CarMessagesMapper.xml" />
< mapper resource = " databases/InOutMapper.xml" />
< mapper resource = " databases/ParkMessagesMapper.xml" />
</ mappers>
</ configuration> ]
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
< 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> org.example</ groupId>
< artifactId> MybatisStudy</ artifactId>
< packaging> pom</ packaging>
< version> 1.0-SNAPSHOT</ version>
< modules>
< module> mybatis01</ module>
</ modules>
< dependencies>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.47</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.2</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
</ dependency>
</ dependencies>
</ project>
3.MybatisUtils
package mybatis. utils ;
import java. io. IOException ;
import java. io. InputStream ;
import org. apache. ibatis. io. Resources ;
import org. apache. ibatis. session. SqlSession ;
import org. apache. ibatis. session. SqlSessionFactory ;
import org. apache. ibatis. session. SqlSessionFactoryBuilder ;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
String resources = "mybatis-config.xml" ;
try {
InputStream inputStream = Resources . getResourceAsStream ( resources) ;
sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
public static SqlSession getSqlSession ( ) {
return sqlSessionFactory. openSession ( ) ;
}
}
4.db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatisuseSSL=true&useUnicode=true&characterEncoding=UTF-8
// 你的用户名
username=root
// 你的密码
password=123456
5.UserMapper.xml(Mapper例子)
<?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.tansty.dao.UserMapper" >
< select id = " getUserList" resultType = " com.tansty.pojo.User" >
select * from mybatis.user
</ select>
< select id = " getUserList2" resultType = " com.tansty.pojo.User" >
select * from mybatis.user where user like #{value}
</ select>
< insert id = " insertUser" parameterType = " com.tansty.pojo.User" >
insert into mybatis.user(id,user,pwd) values(#{id},#{user},#{pwd})
</ insert>
< insert id = " insertUser2" parameterType = " map" >
insert into mybatis.user(id,user,pwd) values(#{id},#{userName},#{password})
</ insert>
< delete id = " deleteUser" parameterType = " int" >
delete from mybatis.user where id=#{id}
</ delete>
< update id = " updateUser" parameterType = " com.tansty.pojo.User" >
update mybatis.user set user = #{user},pwd=#{pwd} where id=#{id} ;
</ update>
</ mapper>
6.资源导出问题解决
< build>
< resources>
< resource>
< directory> src/main/resources</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
</ resource>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
</ resource>
</ resources>
</ build>