在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:quux00/mybatis-koans开源软件地址:https://github.com/quux00/mybatis-koans开源编程语言:Graphviz (DOT) 70.1%开源软件介绍:MyBatis KoansA koan is a question or statement to be meditated upon in order to improve and test a student's progress. Among programmers, software koans have become a clever way to learn a software language or tool. As the Ruby koan website says: "The Koans walk you along the path to enlightenment" -- in this case to learn and practice with the MyBatis 3 data mapper framework. A software koan comes in the form of a broken unit test that you must fix to get it to pass, usually by filling in the blanks or entire missing sections. The koan is intended to teach one or a small set of cohesive features about the language or tool being studied. This set of koans focus on the excellent MyBatis data mapper framework for Java. MyBatis 3 has made significant changes from the previous iBATIS framework and these koans are designed to help you learn how MyBatis 3 works. The structure of these koans is inspired by the challenging and informative Neo4j koans by Jim Webber and colleagues. ContentsOverviewTo do the koans you will need a relational database, the Java JDK, JDBC drivers, JUnit, the MyBatis Persistence Framework, a Java build tool and an editor/IDE. The mybatis-koan setup tries be flexible to allow you to use your build tool, database of choice and run the koans either from the command line or within your IDE. The koans come twice - once in "uncompleted" form and once in "completed" form. The "uncompleted" koans are the ones you will fill in. They are JUnit 4 tests in the The completed koans are there for reference in case you get stuck and need to see the solution and also to test that you have your environment set up. They are in the While MyBatis can be used with other JVM languages, these koans are all in pure Java. To do these koans, the sakila example database was chosen. It is a sample database schema and dataset originally built in MySQL that has been created for many other relational databases. In addition to one-to-many and many-to-many relationships for us to model, it has stored procedures and stored functions that we will learn to access via MyBatis. PrerequisitesMore specifically you must have:
Set up overviewUnfortunately, the setup for the MyBatis koans is not as simple as the Ruby koans, since you have to set up and configure a database, load a standard dataset, and configure the MyBatis system for it. So you'll need to roll up your sleeves a bit before you can get started meditating on the koans themselves. However, we now provide a fast-track: using maven and the H2 database is the fastest way to get going. Using maven with PostgreSQL or MySQL requires only a little more work. Using ant requires a little more work still, since you will need to download and set up the depedencies yourself. Main steps to doing the koansTo give you sense of the flow, here are the steps for getting set up and then working through the koans. In the sections that follow we provide more details. Step 0: Choose the database server and build tool you want to use Step 1: Install any missing prerequisites listed above Step 2: Clone or download the mybatis-koans from GitHub and take a look at the directory structure Step 3: Create the sakila database and load the dataset (not necessary for H2) Step 4: Study the provided sakila database diagrams to get familiar with it Step 5: Run maven to download the dependencies or, if using ant, download the dependencies manually and install them in the koan lib directory Step 6: Run a few of the completed koans to make sure everything is working on your system Step 7: Start working on the koans in your editor / IDE of choice Step 8: Run the koan you are working on to see if it passes the tests Step 9: If you are having trouble completing the koan, research your options online, using the MyBatis User Guide, or as a last resort take a peek at the completed koan we provide. Ideally you will look at the completed koan only after yours is done to see if you came up with a different solution. Step 10: Repeat Steps 8 and 9 until all the koans are finished. Step 11: Think of additional koans that exercise MyBatis features you'd like to learn better. Step 12: Fork this repo on GitHub, write your own and make a pull request to add back to the MyBatis community. SetupWe provide three ways to run the koans (that we have tested):
We have completed the koans for three databases:
You are welcome to do the koans a different database and contribute solutions back. So you will need to decide what options you'd like to take. The closest thing to a "push-button" solution is to use maven and H2. To get the koans set up for that route, the only thing you will need to have pre-installed is maven and Java. Using any other database or ant will require additional setup, as described below. Database
Clone the koans repo (or download it)If you have git installed, clone this repo:
If you don't have git, you can just download a zip or tarball of the koans. The koan directory structureThere are four top-level directories. db:
The sakila database files are here. For H2, we provide the actual binary db file. For MySQL and PostgreSQL, we provide the SQL files for the schema and default dataset. In addition, documentation of the schema is available in the lib: This is where to put jars (or links to jars) if you will be using ant. If you use maven, then you can ignore this directory. scripts: A number of ant "scriptlets" are provided as one way to run individual koans as maven targets within Eclipse. The groovy script that generates the ant scriptlets is also provided. If you are not using maven, you can ignore this directory. src:
The koan source code is organized using maven's default directory structure. Maven splits src into Setup the sakila databaseH2Nothing to do here - the binary database is provided in the PostgreSQL or MySQL : Some assembly requiredTo use PostgreSQL or MySQL you will need to both install the database server and also populate it with the sakila schema and dataset. I provide instructions on how to do this from the command line (tested on a Linux machine): Creating and loading the PostgreSQL sakila database $ cd db/postgresql # create your database user if you haven't already # and then edit the next line to have your username rather than mine $ echo "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO midpeter444;" >> postgres-sakila-schema.sql $ sudo su postgres $ createdb sakila $ psql sakila < postgres-sakila-schema.sql $ psql sakila < postgres-sakila-data.sql $ <Ctrl-D> (log-out as postgres back to your user) $ psql -h localhost sakila # log in here and check that tables were created and can be queried Creating and loading the MySQL sakila database $ cd db/mysql $ mysql -p mysql> create database sakila; Query OK, 1 row affected (0.00 sec) mysql> exit $ mysql sakila -p < sakila-schema.sql $ mysql sakila -p < sakila-data.sql $ mysql -p sakila # log in here and check that the tables were created and that you can query them Other database serversFor other databases, get the sakila schema and dataset from here. Visual aids for the sakila schemaIn the View PNG files in the For a deeper analysis open the Finally, you can also read through the documentation that MySQL provides for the sakila database: http://dev.mysql.com/doc/sakila/en/index.html Build ToolsMaven: I just want to get going!If you have the prerequisites in place, the fastest way to get going is to use maven. H2 is the default database in the pom. If you want to use MySQL or PostgreSQL, uncomment that section of the pom dependencies to get their JDBC driver. If you want to use another database, add its JDBC driver to the maven pom. From the command lineFrom the top dir of the koans, type:
This will download the dependencies for compiling the mybatis koans with H2 and then compile both the incomplete and completed koans. (You will get more dependencies downloaded when you run later targets.) If you see no errors while downloading and compiling, then try running a couple of the completed koans in the "test" directory to see if everything seems to be working. I prefer Apache AntWhen using the ant for the koans, you will not have any dependency management system. You will need to put (or symlink) the jar file dependencies in the
To run with H2, you will also need to have:
So in the end you'll need to have the lib directory have those jars or links to those jars, such as I have here:
Run the completed koansBefore you try running the completed koans, modify the Note: where different solutions were required between MySQL, PostgreSQL and/or H2, we have created additional subdirectories named after the database. mavenTo run individual completed koans from the command line, use this syntax:
This says to use the H2 database and run Koan02. Change the koan name to run different ones. To use mysql or postgresql, change the suffix of the -P argument (you can also change the target from
To run all the tests for a given database, leave off the -D target:
The
Example Output
Ideally, among all the verbiage that maven spits out, when you run a completed koan test, you will see output that includes this:
Note that for the H2 version it starts ("spawns") the H2 database, runs the koan (Koan02 in this case), which has three test targets, and then stops the H2 server. If you choose mysql or postgresql it will just run the koan; it will not start and stop the db server. antMake sure you have ant in your PATH. In the mybatis-koans directory, run To run individual completed koans from the command line, use this syntax:
You can also run all the koans for the database you've chosen with:
Note: If you are using H2, you need to first run the ant target Tweaking the ant settingsBy default, the koan test output will be written to the console only. If you want the output to be written to a plain text formatted file in the top directory called
Do the Koans in EclipseAfter you install the m2e plugin, do: Import > Maven > Existing Maven Project. Specify the mybatis-koans directory for the "root directory". It should find the pom.xml and name the project "mybatis-koans". Click Finish. It should find your maven dependencies and download those or reference them if you've already done the maven commands from the command line. If something doesn't work, try going to the command line and type Once you have the mybatis-koans project created in Eclipse and have all the previous steps done (such as jar dependencies in place), you can run the koans (the main ones you will do and the already completed ones) by:
Modify the config.properties in src/main and src/test to set the database properties you need. (For H2, you don't need to change it.) If you are using the H2 database and using the Eclipse JUnit test runner or ant targets, you must first start the H2 database: in The other option is to start H2 with ant by invoking the runH2 target. Example Instructions: Run koans as Eclipse JUnit tests Navigate to If you get the error Example Instructions: Run koans from the ant targets in Eclipse To run the koans from the Ant targets, first open the Ant view: Window > Show View > Ant. Drag build.xml to the Ant view window. You will see all the ant targets displayed. If you are using H2, make sure you have the RunH2 programming running, either by runnign it from Eclipse (see above section) or running it with the ant runH2 target from the command line. Next double click the Example Instructions: Run koans from the maven targets in Eclipse You can either set up the targets one by one by editing the Run As -> Maven Build option provided by m2e. I don't recommend it, but here is a reference that gives a little more information: http://stackoverflow.com/a/2808748/871012 Instead, in the scripts directory, we provide a bunch of little ant scriptlets that will run each maven target. To make this work, first edit Navigate to the scripts that match the db you are using and whether you want to run the completed koan or the one in main that you are working on. For example, In Eclipse, right click (or double click to open and right click) one of these .ant scriptlets, such as Caution: Overall, within Eclipse I recommend that you run the koans using Eclipse's JUnit runner as it will give you nice red/green output. Currently, when these scriptlets run it is very hard to tell if a test fails. Maven will report an error, but the ant scriptlet still reports SUCCESS like so:
If anyone has a patch to these scriptlets to fix this inconsistency, I'd welcome that. Do the koansOnce you are satisfied that the completed koans work, you should then begin to work on the incomplete koans. Start with Koan01 in the Before you start, modify the Whenever you start a new koan, open the file KoanXX.java and read the overview and instructions in the javadoc comments at the top of the file. The goal is to fill in all the TODOs that get the tests already set up to pass. Test your koansTo run the koans you are completing, do:
or
or run them from within Eclipse as JUnit tests. Adjust logging verbosityIf you want more logging information when running your koan tests, adjust the logging levels in the To have less logging (none if the koans passing), set logging levels to "info". For more, set levels to "debug". Mix and match to get the output you want. tl;dr - Too many options, just tell me the bare minimumWe've tried to be flexible with options and document everything, but here's my short version for those that want my recommendation on how I would do it:
For extra credit, think of new koans and add them to the repo with a pull request! Directory of Koan TopicsSee the directory of koans for a description of what each koan tests. A Note on SolutionsFor many koans, there are probably many ways to make it work (even within the constraints put in place to exercise a given feature of MyBatis). If you have an alternative solution that will help others see the possibilities, feel free to send a pull request to get it added to the repo. A Note on Best PracticesAs you go through the koans, you'll see that I change styles/idioms from time to time. Sometimes I start a session for each test, then later not. Sometimes I use mappers with a Java interface, sometimes not. Sometimes I turn camelCase mapping on, sometimes not. These koans are not intended to recommend best practice, but rather to see the variations of possibilities that MyBatis 3 allows. I mix it up so you can be reminded of these variations. You should decide, in conjunction with the recommendations in the User Guide and reading other tutorials and code examples, on what is best practice for your code base. Current Status07-Mar-2013: mybatis-3.2 was released in late February. The koans dependencies have been updated to work with mybatis-3.2. Basically, cglib (2.2.2) and log4j (1.2.17) had to be explicitly added to the classpath. They were added to the pom and the instructions below for adding them to your lib directory if using Ant have also been updated. If you need to get back to the mybatis-3.1 version, I have created a git tag for that last commit:
Note: In mybatis-3.2, SQLBuilder has been deprecated, so Koan18 is now vestigial. However, I will leave it in for a while longer in case people are still using older versions and depending on SQLBuilder. You should stop using it. See the MyBatis documentation for more details. 17-Feb-2013: José Antonio López figured out how to implement the EmailTypeHandler for the Null Object Pattern koan (Koan 19). See his stackoverflow posting. We should add that as an alternative solution to the completed koans. Also, mybatis-3.2.0-SNAPSHOT recently came out. Anyone tested the koans with that yet? Put that on our TODO list. 08-Dec-2012: Thiago Arrais fixed a bug in Koan02. I fixed a few minor ambiguities in the PostgreSQL sakila installation instructions. 12-Aug-2012: Added Koan 26. 05-Aug-2012: Two new koans (24 and 25) were added. The original koans were released in May 2012. In July, Andrei Pozolotin contributed some changes to make the koans work with maven, so we are releasing the revised setup for the koans. So far, we have tested them carefully with MyBatis-3.1.1 using H2-1.3.168, PostgreSQL 9.1.3 and MySQL 5.5 on Linux and using H2 on Windows 7. If you try them with other combinations and have problems, let me know. (Suggested patches ar |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论