一、SqlMapConfig.xml配置文件:
1)SqlMapConfig.xml中配置的内容和顺序:
2)properties(属性):
有两种方式:
第一种:
<properties>
<property name="jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="jdbc.url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="jdbc.username" value="root"/>
<property name="jdbc.password" value="root"/>
</properties>
第二种:在resources目录下新建db.properties
文件。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
此时properties属性配置如下:
<properties resource="db.properties"/>
配置完成后,我们需要引用配置:
<!--配置mybatis环境-->
<environments default="mysql">
<!--配置mysql环境-->
<environment id="mysql">
<!--配置事务类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据库信息:用的是数据源(连接池)-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
3)typeAliases(类型别名):
<typeAliases>
<!--单个别名定义-->
<!-- <typeAlias type="com.zl.domain" alias="user"/>-->
<!-- 批量别名定义,扫描整个包下的类,别名为类名(不区分大小写) -->
<package name="com.zl.domain"/>
</typeAliases>
4)mappers(映射器):
<mappers>
<!--使用相对于类路径的资源-->
<!--<mapper resource="com/zl/dao/UserDao.xml" />-->
<!--使用mapper接口类路径-->
<!-- <mapper class="com.zl.dao.UserDao"/>-->
<!-- 注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。-->
<!--注册指定包下的所有mapper接口-->
<package name="com.zl.dao"/>
<!--注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。-->
</mappers>
二、常规CRUD操作:
User实体类:
package com.zl.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
UserDao接口:
package com.zl.dao;
import com.zl.domain.User;
import java.util.List;
public interface UserDao {
/**
* 查询所有用户
* @return
*/
List<User> findAll();
/**
* 根据id查询用户
* @param id
* @return
*/
User findById(Integer id);
/**
* 保存用户
* @param user
*/
void saveUser(User user);
/**
* 更新用户
* @param user
*/
void updateUser(User user);
/**
* 根据id删除用户
* @param id
*/
void deleteUser(Integer id);
/**
* 根据名称模糊查询
* @param name
* @return
*/
List<User> findLikeName(String name);
/**
* 查询总记录数
* @return
*/
Integer findTotalCount();
}
UserDao.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.zl.dao.UserDao">
<!--查询所有-->
<select id="findAll" resultType="user">
select * from user
</select>
<!--根据id查询用户-->
<select id="findById" resultType="user" parameterType="int">
select * from user where id = #{id}
</select>
<!--保存用户-->
<select id="saveUser" parameterType="user">
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
</select>
<!--更新用户-->
<select id="updateUser" parameterType="user">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
</select>
<!--根据id删除用户-->
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
<select id="findLikeName" resultType="user" parameterType="String">
select * from user where username like #{username}
</select>
<select id="findTotalCount" resultType="int">
select count(*) from user
</select>
</mapper>
主配置文件SqlMapConfig.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"/>
<typeAliases>
<!--单个别名定义-->
<!-- <typeAlias type="com.zl.domain" alias="user"/>-->
<!-- 批量别名定义,扫描整个包下的类,别名为类名(不区分大小写) -->
<package name="com.zl.domain"/>
</typeAliases>
<!--配置mybatis环境-->
<environments default="mysql">
<!--配置mysql环境-->
<environment id="mysql">
<!--配置事务类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据库信息:用的是数据源(连接池)-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--使用相对于类路径的资源-->
<!--<mapper resource="com/zl/dao/UserDao.xml" />-->
<!--使用mapper接口类路径-->
<!-- <mapper class="com.zl.dao.UserDao"/>-->
<!-- 注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。-->
<!--注册指定包下的所有mapper接口-->
<package name="com.zl.dao"/>
<!--注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。-->
</mappers>
</configuration>
Maven主配置文件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>com.zl</groupId>
<artifactId>hello_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
网友评论