亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

spring 集成 mybatis的實例詳解

瀏覽:33日期:2023-07-26 18:59:06
環境配置

1>先創建maven的quickstart項目;并且創建dao層,service層,controller層,po層,mapper,resources以及下面的配置文件(db.properties,log4j.properties,mybatis.xml,spring.xml).

spring 集成 mybatis的實例詳解

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(日志打印);

設置資源目錄和插件

<build> <!-- Maven 項目:如果源代碼(src/main/java)存在xml、properties、tld 等文件 Maven 默認不會自動編譯該文件到輸出目錄,如果要編譯源代碼中xml properties tld 等文件 需要顯式配置 resources 標簽 --> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.tld</include> </includes> <filtering>false</filtering> </resource> </resources> </build>

3>配置spring.xml

<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xmlns:context='http://www.springframework.org/schema/context' xmlns:aop='http://www.springframework.org/schema/aop'xmlns:tx='http://www.springframework.org/schema/tx' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd'> <!-- 掃描基本包 --> <context:component-scan base-package='com.xxxx' /> <!-- 加載properties 配置文件 --> <context:property-placeholder location='classpath:db.properties' /> <!-- aop --> <aop:aspectj-autoproxy /> <!-- 配置c3p0 數據源 --> <bean class='com.mchange.v2.c3p0.ComboPooledDataSource'> <property name='driverClass' value='${jdbc.driver}'></property> <property name='jdbcUrl' value='${jdbc.url}'></property> <property name='user' value='${jdbc.username}'></property> <property name='password' value='${jdbc.password}'></property> </bean> <!-- 配置事務管理器 --> <bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'> <property name='dataSource' ref='dataSource'></property> </bean> <!-- 設置事物增強 --> <tx:advice transaction-manager='txManager'> <tx:attributes> <tx:method name='add*' propagation='REQUIRED' /> <tx:method name='insert*' propagation='REQUIRED' /> <tx:method name='update*' propagation='REQUIRED' /> <tx:method name='delete*' propagation='REQUIRED' /> </tx:attributes> </tx:advice> <!-- aop 切面配置 --> <aop:config> <aop:pointcut expression='execution(* com.xxxx.service..*.*(..))' /> <aop:advisor advice-ref='txAdvice' pointcut-ref='servicePointcut' /> </aop:config> <!-- 配置 sqlSessionFactory --> <bean class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='dataSource'></property> <property name='configLocation' value='classpath:mybatis.xml' /> <property name='mapperLocations' value='classpath:com/xxxx/mapper/*.xml' /> </bean> <!-- 配置掃描器 --> <bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'> <!-- 掃描com.xxxx.dao這個包以及它的子包下的所有映射接口類 --> <property name='basePackage' value='com.xxxx.dao' /> <property name='sqlSessionFactoryBeanName' value='sqlSessionFactory' /> </bean></beans>

4>配置 mybatis.xml

<?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> <!-- 定義類別名 --> <typeAliases> <package name='com.xxxx.po'/> </typeAliases></configuration>

5>配置 db.properties

jdbc.url中?前面的spring_mybatis是數據庫名字,注意要修改下

password是密碼,也是要修改下的

6>添加日志

jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/spring_mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=falsejdbc.username=rootjdbc.password=root

log4j.properties

log4j.rootLogger=DEBUG, Console# Consolelog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%nlog4j.logger.java.sql.ResultSet=INFOlog4j.logger.org.apache=INFOlog4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.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 updateDate; /** set get toString 方法省略 **/}

2>在dao層添加UserDao接口

public interface UserDao { User queryUserByUserId(Integer userId);}

3>在mapper包添加UserMapper.xml 映射文件

sql代碼寫在這地方

<?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.xxxx.dao.UserDao'> <select parameterType='int'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} </select></mapper>

4>添加 UserService.java

@Servicepublic class UserService { @Autowired private UserDao userDao; public User queryUserByUserId(Integer userId){ return userDao.queryUserByUserId(userId); }}

5>添加 UserController.java

@Controllerpublic 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()); }}

到此這篇關于spring 集成 mybatis的文章就介紹到這了,更多相關spring 集成 mybatis內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 深夜国产一区二区三区在线看 | 欧美一区二区三区性 | 亚洲成人在线播放视频 | 国产一区二区免费视频 | a级毛片高清免费视频 | 亚洲在线观看一区二区 | 日日综合网 | 日本在线一级 | 黄片毛片在线观看 | 国产在线观看第一页 | 免费一区二区三区四区五区 | 好叼操这里只有精品 | 国产色司机在线视频免费观看 | 久久国产免费观看精品3 | 婷婷综合激情 | 伊人久久视频 | 久久综合九色综合桃花 | 99久久久国产精品免费牛牛四川 | 亚洲天天做日日做天天看2018 | 欧美亚洲一区二区三区在线 | 成 人色 网 站999 | 久久精品成人免费网站 | 国产黄色大片在线观看 | 亚洲邪恶天堂影院在线观看 | 欧美精品一区二区三区在线播放 | 小明看片| 欧美成人艳星在线播放 | 国产拍拍视频一二三四区 | 国产毛片儿 | 小明看片成人永久在线观看 | 国产精品国产三级国产专不∫ | 久久羞羞| 大乳一级一区二区三区 | 性激烈的欧美暴力三级视频 | 麻豆果冻传媒精品二三区 | 久草网视频在线 | 亚洲欧美精品国产一区色综合 | 国产精品天天看天天爽 | 中文日产国产精品久久 | 精品国产麻豆免费网站 | 国产中日韩一区二区三区 |