2、创建SqlSession完成基于配置的数据访问操作,完成基于注解的数据操作。 3、使用动态SQL语法,设计并实验一个多条件查询的功能模块。
时间: 2023-12-14 19:39:26 浏览: 83
2、创建SqlSession完成基于配置的数据访问操作,完成基于注解的数据操作。
基于配置的数据访问操作:
1. 首先需要在mybatis配置文件中配置数据源和相关的mapper文件路径。
2. 在Java代码中通过SqlSessionFactoryBuilder构建SqlSessionFactory,然后通过SqlSessionFactory创建SqlSession。
3. 在SqlSession中调用相关的CRUD方法进行数据访问操作。
示例代码:
mybatis-config.xml 配置文件:
```xml
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
```
UserMapper.xml 配置文件:
```xml
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserById" parameterType="int" resultType="com.example.model.User">
select * from user where id = #{id}
</select>
<insert id="saveUser" parameterType="com.example.model.User" keyProperty="id" useGeneratedKeys="true">
insert into user (name, age) values (#{name}, #{age})
</insert>
<update id="updateUser" parameterType="com.example.model.User">
update user set name = #{name}, age = #{age} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
</mapper>
```
Java代码:
```java
public class UserDaoImpl implements UserDao {
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public User getUserById(int id) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
return userMapper.getUserById(id);
}
}
@Override
public void saveUser(User user) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.saveUser(user);
sqlSession.commit();
}
}
@Override
public void updateUser(User user) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.updateUser(user);
sqlSession.commit();
}
}
@Override
public void deleteUser(int id) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser(id);
sqlSession.commit();
}
}
}
```
基于注解的数据操作:
1. 在Mapper接口中使用相应的注解标注SQL语句。
2. 在Java代码中通过SqlSessionFactoryBuilder构建SqlSessionFactory,然后通过SqlSessionFactory创建SqlSession。
3. 在SqlSession中调用相关的CRUD方法进行数据访问操作。
示例代码:
```java
public interface UserMapper {
@Select("select * from user where id = #{id}")
User getUserById(int id);
@Insert("insert into user (name, age) values (#{name}, #{age})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void saveUser(User user);
@Update("update user set name = #{name}, age = #{age} where id = #{id}")
void updateUser(User user);
@Delete("delete from user where id = #{id}")
void deleteUser(int id);
}
public class UserDaoImpl implements UserDao {
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public User getUserById(int id) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
return userMapper.getUserById(id);
}
}
@Override
public void saveUser(User user) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.saveUser(user);
sqlSession.commit();
}
}
@Override
public void updateUser(User user) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.updateUser(user);
sqlSession.commit();
}
}
@Override
public void deleteUser(int id) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser(id);
sqlSession.commit();
}
}
}
```
3、使用动态SQL语法,设计并实验一个多条件查询的功能模块。
动态SQL语法可以根据不同的条件生成不同的SQL语句,实现多条件查询功能。
示例代码:
```xml
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserList" parameterType="java.util.Map" resultType="com.example.model.User">
select * from user where 1 = 1
<if test="name != null and name != ''">
and name like concat('%', #{name}, '%')
</if>
<if test="age != null">
and age = #{age}
</if>
<if test="start != null and end != null">
and create_time between #{start} and #{end}
</if>
</select>
</mapper>
```
Java代码:
```java
public interface UserMapper {
List<User> getUserList(Map<String, Object> params);
}
public class UserDaoImpl implements UserDao {
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public List<User> getUserList(String name, Integer age, Date start, Date end) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> params = new HashMap<>();
params.put("name", name);
params.put("age", age);
params.put("start", start);
params.put("end", end);
return userMapper.getUserList(params);
}
}
}
```
阅读全文