创建Mybatis项目流程
Mybatis帮助文档:
https://mybatis.org/mybatis-3/zh/getting-started.html
1.在IDEA创建maven项目
2.在pom.xml中导入jar
<?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"> <parent> <artifactId>mybatis-study</artifactId> <groupId>com.mybatis</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>mybatis-01</artifactId> <dependencies> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build><!--防止XML文件不在resources文件内无法读取--> <resources> <resource> <directory>src/main/resource</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>
3.在resources下创建mybatis-config.xml并且配置mysql信息
<?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> <!--让mapper.xml的resultType简化--> <typeAliases> <package name="com.peng.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!--我装的mysql是8.0.26版本的driber与url需要添加'.cj'和注意时区--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306?mybatis&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <!--数据库绑定Sql语句必须有--> <mapper resource="com/mybatis/dao1/UserMapper.xml" ></mapper> </mappers> </configuration>
4.创建mybatis工具类获取sqlSessionFactroy对象
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { //使用mybatis第一步获取sqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { e.printStackTrace(); } } //有了sqlSessionFactory我们就可以从中获取SqlSession的实例了 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); // SqlSession 中包含了面向数据库执行sql命令所需要的所有方法. } }
5.创建UserDao接口并用XML实现
UserDao import com.mybatis.pojo.User; import java.util.List; public interface UserDao { //全部查询 List<User> getUserList(); //根据i的查询 User getUserById(int id); //增加 Integer addUser(User user); //修改 Integer updateUser(User user); //删除 Integer deleteUser(int id); }
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.mybatis.dao1.UserDao"> <!--namespace=绑定一个对应的Dao/Mapper接口--> <select id="getUserList" <!--此处id为要实现的方法--> resultType="User"> <!--返回结果指向实体类--> select * from mybatis.user </select> <select id="getUserById" parameterType="int" resultType="User"> select * from mybatis.user where id=#{id} </select> <select id="addUser" parameterType="User"> insert into mybatis.user (id,name,pwd)values (#{id},#{name},#{pwd}); </select> <select id="updateUser" parameterType="User"> update mybatis.user set name=#{name},pwd=#{pwd} where id =#{id}; </select> <select id="deleteUser" parameterType="User" resultType="int"> delete from mybatis.user where id=#{id}; </select> </mapper>
6.测试类
public class UserDaoTest { @Test public void test(){ SqlSession sqlSession= MybatisUtils.getSqlSession(); UserMapper userDao = sqlSession.getMapper(UserMapper.class); List<User> userList = userDao.getUserList(); for(User user :userList){ System.out.println(user); } System.out.println("查询成功!"); sqlSession.close(); } @Test public void getUserById(){ SqlSession sqlSession=MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); System.out.println(user); System.out.println("查询成功!"); sqlSession.close(); } @Test public void addUser(){ SqlSession sqlSession=MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.addUser(new User(5,"李蛋","123123")); System.out.println("输入成功!"); sqlSession.close(); } @Test public void updateUser(){ SqlSession sqlSession=MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(4,"湛恒言","12312")); System.out.println("修改成功!"); sqlSession.close(); } @Test public void deleteUser(){ SqlSession sqlSession=MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(4); System.out.println("删除成功!"); sqlSession.close(); } }
总结:
1.先导jar包—>操作pom.xml文件
2.创建工具类
3.创建数据库配置文件并在工具类中配置
4.写实体类–>set()get()toString()方法
5.写接口
6.通过xxxMapper.xml文件实现接口
7.测试类
Map,如果我们的实体类或者数据库中的表、字段或者参数过多我们应当考虑使用Map!
接口:
User addUser2(Map<String,Object> map);
Mapper.xml的sql语句
<select id="addUser2" resultType="map" parameterType="com.mybatis.pojo.User"> insert into mybatis.user (id, name, pwd) VALUES (#{userid},#{username},#{userpwd}); </select>
测试类:
@Test public void addUser2(){ SqlSession sqlSession =MybatisUtils.getSqlSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); HashMap<String,Object> map=new HashMap<String, Object>(); map.put("userid",6); map.put("username","王二"); map.put("userpwd","2222"); mapper.addUser2(map); sqlSession.close(); }