Mybatis 注解方式
-
添加依赖与插件
-
使用插件解决xml文件不能放入java包下的问题
<!--可以将xml文件建于java包下并且可被映射--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
-
相关依赖
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--帮助pojo快速生成getter和setting--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
-
-
配置Mybatis.xml配置文件
-
使用properties标签导入db.properties的驱动设置,设置environments标签属性
# db.properties文件 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3307/shop_mybatis username=root password=970809
<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>
-
使用settings标签添加第三方LOG4J日志功能
<settings> <setting name="logImpl" value="LOG4J"/> </settings>
# log4j.properties文件,文件名固定不可变 # 全局日志配置 # 日志有四个级别 :error,warn,info,debug log4j.rootLogger=ERROR, stdout # MyBatis 日志配置 ,可自行设置追踪路径 log4j.logger.dao=TRACE # 控制台输出 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
-
使用typeAliases标签 加入别名设置
<typeAliases>s <package name="pojo"/> </typeAliases>
-
映射器配置,可扫描dao包下的所有接口,如果有同名的映射配置也可扫描
<mappers> <package name="dao"/> </mappers>
-
完整配置
<?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"/> <!--添加日志功能,STDOUT_LOGGING不需要添加第三方jar包--> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <package name="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> </environments> <mappers> <!-- <mapper resource="com/mapper/OrderMapper.xml"/>--> <!-- <mapper resource="com/mapper/UserMapper.xml"/>--> <!-- <mapper resource="com/mapper/DetailMapper.xml"/>--> <!-- <mapper resource="com/mapper/ProductMapper.xml"/>--> <!-- <mapper resource="com/mapper/TypeMapper.xml"/>--> <!-- <mapper class="dao.IUserDao2"/>--> <package name="dao"/> </mappers> </configuration>
-
-
dao接口
直接添加注解@Select,@Insert,@Update,@Delete
如果传入一个参数,使用#{任意字符}获取
如果传入多个参数,使用#{arg0},#{arg1}获取
或者直接传入对象,使用#{对应的参数名}获取
/** * 查询所有的User对象 * @return User对象的列表 */ @Select("select *from user") List<User> getAllUsers(); /** * 新增一个user对象 * @param user * @return */ @Insert("insert into users values(#{uid},#{name},#{pass},#{phone})") int saveUser(User user);
-
测试类
public class TestUserDao { private SqlSession session = null; @Before public void setUp() { session = SessionUtils.getSession(); } @After public void tearDown() { SessionUtils.closeSession(); } @Test public void testGetAllUsers() { IUserDao mapper = session.getMapper(IUserDao2.class); List<User> users = mapper.getAllUsers(); for (User user : users) { System.out.println(user); } } @Test public void testSaveUser() { IUserDao2 userDao = session.getMapper(IUserDao2.class); User u = new User("3", "cpj", "123", "12312312"); userDao.saveUser(u); } }