Mybatis注解开发
Mybatis注解开发其实就是可以省略掉我们的映射文件,直接在接口上编写sql,虽然很方便,但是复杂的sql语句还是使用xml文件来编写更好
首先,我们需要先创建一个Maven项目,可参考我之前编写的Maven项目的创建
1.导入相关依赖包
<!-- Mybatis核心 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.3</version> </dependency> <!-- lombok插件 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency>
2.创建MyBatis的核心配置文件
先配置一个数据库配置文件db.properties,配置文件存放的位置是src/main/resources目录下
driver=com.mysql.jdbc.Driver url=jdbc:mysql://192.168.111.131:3306/mybatis?characterEncoding=UTF-8 username=root password=123456
然后创建MyBatis核心配置文件,习惯上命名为mybatis-config.xml,核心配置文件存放的位置是src/main/resources目录下
<?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"/> <settings> <!--实体类驼峰命名与数据库_命名映射开启--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!--设置连接数据库的环境--> <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 class="com.chen.mybatis.mapper.UserMapper" /> </mappers> </configuration>
3.创建实体类
该地方我们使用的是Lombok配置实体类,可以通过Lombok的配置使用对其进行了解
@AllArgsConstructor @NoArgsConstructor @Data public class User { private Integer id; private String username; private String password; private Integer age; private String sex; private String email; }
4.创建Mapper接口
MyBatis中的mapper接口相当于以前的dao。在Mybatis注解开发中,我们的sql语句也写在该地方。
public interface UserMapper { @Select("select * from t_user") List<User> getUser(); @Select("select * from t_user where id = #{id}") User getUsersById(@Param("id")int id); @Insert("insert into t_user values(#{id},#{username},#{password},#{age},#{sex},#{email})") int addUser(User user);
}
5.编写Mybatis工具类
public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { //读取MyBatis的核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //创建SqlSessionFactoryBuilder对象 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory,生产SqlSession对象 sqlSessionFactory = sqlSessionFactoryBuilder.build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ //参数true为设置自动提交 return sqlSessionFactory.openSession(true); } }
6.测试
getUser语句
public class MybatisTest { @Test public void testMybatis() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUser(); for(User user:userList){ System.out.println(user); } sqlSession.close(); } }
getUsersById语句
public class MybatisTest { @Test public void testMybatis() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUsersById(3); System.out.println(user); sqlSession.close(); } }
addUser语句
public class MybatisTest { @Test public void testMybatis() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.addUser(new User(10,"admin","123",11,"man","123@qq.com")); sqlSession.close(); } }