洞察提升小程序标签管理,实现高效的金融行业数字化转型
512
2023-02-22
spring 集成 mybatis的实例详解
环境配置
1>先创建maven的quickstart项目;并且创建dao层,service层,controller层,po层,mapper,resources以及下面的配置文件(db.properties,log4j.properties,mybatis.xml,spring.xml).
2>配置pom.xml
修改jdk版本;
添加依赖:
junit版本改为4.12;spring-context;spring-test;spring-jdbc;spring-tx(事务);aspectjweaver(切面编程);c3p0(连接池);mybatis;mybatis-spring;mysql-connector-java(mysql驱动包);slf4j-log4j12,slf4j-api(日志打印);
203; 设置资源目录和插件
3>配置spring.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframewohttp://rk.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd"> class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframewohttp://rk.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
4>配置 mybatis.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5>配置 db.properties
jdbc.url中?前面的spring_mybatis是数据库名字,注意要修改下
password是密码,也是要修改下的
6>添加日志
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_mybatis?
useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
jdbc.username=root
jdbc.password=root
log4j.properties
log4j.rootLogger=DEBUG, Console
# Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
添加源代码
1>在po 包下创建 JavaBean 文件 User.java
public class User {
private Integer userId;
private String userName;
private String userPwd;
private String userEmail;
private Date createDate;
private Date updahttp://teDate;
/**
set get toString 方法省略
**/
}
2>在dao层添加UserDao接口
public interface UserDao {
User queryUserByUserId(Integer userId);
}
3>在mapper包添加UserMapper.xml 映射文件
sql代码写在这地方
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
resultType="com.xxxx.po.User">
select user_id as userId,user_name as userName,user_pwd as userPwd
from tb_user
where user_id = #{userId}
4>添加 UserService.java
@Service
public class UserService {
@Autowired
private UserDao userDao;
public User queryUserByUserId(Integer userId){
return userDao.queryUserByUserId(userId);
}
}
5>添加 UserController.java
@Controller
public class UserController {
// 注入userService
@Resource
private UserService userService;
/**
* 通过用户ID查询用户对象
* @param userId
* @return
*/
public User queryUserByUserId(Integer userId) {
User user = userService.queryUserByUserId(userId);
return user;
}
}
执行测试
public class App {
public static void main(String[] args) {
// 加载Spring的配置
BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
// 得到UserController对象
UserController userController = (UserController)
factory.getBean("userController");
// 调用方法
User user = userController.queryUserByUserId(1);
System.out.println(user.toString());
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~